Sum of middle row and column in Matrix
Last Updated :
25 Mar, 2023
Given an integer matrix of odd dimensions (3 * 3, 5 * 5). then the task is to find the sum of the middle row & column elements.
Examples:
Input : 2 5 7
3 7 2
5 6 9
Output : Sum of middle row = 12
Sum of middle column = 18
Input : 1 3 5 6 7
3 5 3 2 1
1 2 3 4 5
7 9 2 1 6
9 1 5 3 2
Output : Sum of middle row = 15
Sum of middle column = 18
Implementation:
CPP
#include <iostream>
using namespace std;
const int MAX = 100;
void middlesum( int mat[][MAX], int n)
{
int row_sum = 0, col_sum = 0;
for ( int i = 0; i < n; i++)
row_sum += mat[n / 2][i];
cout << "Sum of middle row = "
<< row_sum<<endl;
for ( int i = 0; i < n; i++)
col_sum += mat[i][n / 2];
cout << "Sum of middle column = "
<< col_sum;
}
int main()
{
int mat[][MAX] = {{2, 5, 7},
{3, 7, 2},
{5, 6, 9}};
middlesum(mat, 3);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int MAX = 100 ;
static void middlesum( int mat[][], int n)
{
int row_sum = 0 , col_sum = 0 ;
for ( int i = 0 ; i < n; i++)
row_sum += mat[n / 2 ][i];
System.out.println ( "Sum of middle row = "
+ row_sum);
for ( int i = 0 ; i < n; i++)
col_sum += mat[i][n / 2 ];
System.out.println ( "Sum of middle column = "
+ col_sum);
}
public static void main (String[] args) {
int mat[][] = {{ 2 , 5 , 7 },
{ 3 , 7 , 2 },
{ 5 , 6 , 9 }};
middlesum(mat, 3 );
}
}
|
Python3
def middlesum(mat,n):
row_sum = 0
col_sum = 0
for i in range (n):
row_sum + = mat[n / / 2 ][i]
print ( "Sum of middle row = " ,
row_sum)
for i in range (n):
col_sum + = mat[i][n / / 2 ]
print ( "Sum of middle column = " ,
col_sum)
mat = [[ 2 , 5 , 7 ],
[ 3 , 7 , 2 ],
[ 5 , 6 , 9 ]]
middlesum(mat, 3 )
|
C#
using System;
class GFG {
static void middlesum( int [,]mat, int n)
{
int row_sum = 0, col_sum = 0;
for ( int i = 0; i < n; i++)
row_sum += mat[n / 2, i];
Console.WriteLine ( "Sum of middle row = "
+ row_sum);
for ( int i = 0; i < n; i++)
col_sum += mat[i, n / 2];
Console.WriteLine ( "Sum of middle column = "
+ col_sum);
}
public static void Main () {
int [,]mat = {{2, 5, 7},
{3, 7, 2},
{5, 6, 9}};
middlesum(mat, 3);
}
}
|
PHP
<?php
function middlesum( $mat , $n )
{
$row_sum = 0; $col_sum = 0;
for ( $i = 0; $i < $n ; $i ++)
$row_sum += $mat [ $n / 2][ $i ];
echo "Sum of middle row = "
, $row_sum , "\n" ;
for ( $i = 0; $i < $n ; $i ++)
$col_sum += $mat [ $i ][ $n / 2];
echo "Sum of middle column = "
, $col_sum ;
}
$mat = array ( array (2, 5, 7),
array (3, 7, 2),
array (5, 6, 9));
middlesum( $mat , 3);
?>
|
Javascript
<script>
var MAX = 100;
function middlesum(mat , n)
{
var row_sum = 0, col_sum = 0;
for (i = 0; i < n; i++)
row_sum += mat[parseInt(n / 2)][i];
document.write(
"Sum of middle row = " + row_sum+ "<br/>"
);
for (i = 0; i < n; i++)
col_sum += mat[i][parseInt(n / 2)];
document.write(
"Sum of middle column = " + col_sum
);
}
var mat = [ [ 2, 5, 7 ],
[ 3, 7, 2 ],
[ 5, 6, 9 ] ];
middlesum(mat, 3);
</script>
|
Output
Sum of middle row = 12
Sum of middle column = 18
Time Complexity : O(n)
Auxiliary Space: O(1) using constant space to initialize row_sum and col_sum variables, since no extra space has been taken.
Using Stl:
Here we use the accumulate function to do it.
For JavaScript:
Here we use the reduce function to do it.
C++
#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
vector<vector< int >>v{{2, 5, 7},
{3, 7, 2},
{5, 6, 9}};
int n=v.size();
cout<< "The sum of all the element in middle row : " <<endl;
cout<<accumulate(v[n/2].begin(),v[n/2].end(),0)<<endl;
for ( int i=0;i<n;i++)
for ( int j=i+1;j<n;j++)
swap(v[i][j],v[j][i]);
cout<< "The sum of all the element in middle column : " <<endl;
cout<<accumulate(v[n/2].begin(),v[n/2].end(),0);
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int [][] v = {{ 2 , 5 , 7 },
{ 3 , 7 , 2 },
{ 5 , 6 , 9 }};
int n = v.length;
System.out.println( "The sum of all the element in middle row : " );
System.out.println(Arrays.stream(v[n/ 2 ]).sum());
for ( int i = 0 ; i < n; i++)
for ( int j = i + 1 ; j < n; j++) {
int temp = v[i][j];
v[i][j] = v[j][i];
v[j][i] = temp;
}
System.out.println( "The sum of all the element in middle column : " );
System.out.println(Arrays.stream(v[n/ 2 ]).sum());
}
}
|
Python3
v = [[ 2 , 5 , 7 ],
[ 3 , 7 , 2 ],
[ 5 , 6 , 9 ]]
n = len (v)
print ( "The sum of all the element in middle row : " )
print ( sum (v[n / / 2 ]))
for i in range (n):
for j in range (i + 1 ,n):
v[i][j],v[j][i] = v[j][i],v[i][j]
print ( "The sum of all the element in middle column : " )
print ( sum (v[n / / 2 ]))
|
Javascript
var v = [[2, 5, 7],
[3, 7, 2],
[5, 6, 9]];
var n = v.length;
console.log( "The sum of all the element in middle row : " );
console.log(v[Math.floor(n/2)].reduce((a,b)=>a+b,0));
for ( var i=0;i<n;i++)
for ( var j=i+1;j<n;j++)
[v[i][j],v[j][i]] = [v[j][i],v[i][j]];
console.log( "The sum of all the element in middle column : " );
console.log(v[Math.floor(n/2)].reduce((a,b)=>a+b,0));
|
C#
using System;
using System.Collections.Generic;
using System.Linq;
class Program {
static void Main( string [] args) {
List<List< int >> v = new List<List< int >> {
new List< int > { 2, 5, 7 },
new List< int > { 3, 7, 2 },
new List< int > { 5, 6, 9 }
};
int n = v.Count;
Console.WriteLine( "The sum of all the element in middle row : " );
Console.WriteLine(v[n / 2].Sum());
for ( int i = 0; i < n; i++) {
for ( int j = i + 1; j < n; j++) {
int temp = v[i][j];
v[i][j] = v[j][i];
v[j][i] = temp;
}
}
Console.WriteLine( "The sum of all the element in middle column : " );
Console.WriteLine(v[n / 2].Sum());
}
}
|
Output
The sum of all the element in middle row :
12
The sum of all the element in middle column :
18
The accumulated value of f applications. Complexity: O(n×k), where n is the distance from first to last , O(k) is complexity of f function.
Time Complexity: O(n*k).
Auxiliary Space : O(k)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...