Open In App

Sum of middle row and column in Matrix

Last Updated : 20 Nov, 2025
Comments
Improve
Suggest changes
5 Likes
Like
Report

Given a square matrix mat[][] of odd dimensions, find the sum of the elements in the middle row and the middle column.

Examples: 

Input : mat[][] = [[2, 5, 7],
[3, 7, 2],
[5, 6, 9]]
Output : [12, 18]
Explanation: Middle row [3, 7, 2] sums to 12, middle column [5, 7, 6] sums to 18.

Input : mat[][] = [[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 : [15, 18]
Explanation: Middle row [1, 2, 3, 4, 5] sums to 15, middle column [5, 3, 3, 2, 5] sums to 18.

[Approach] By Iterative Traversal – O(n) Time and O(1) Space

We will first find the middle index mid = n / 2. We then sum all elements in the middle row and all elements in the middle column to get rowSum and colSum. Finally, we return [rowSum, colSum].

C++
//Driver Code Starts
#include <iostream>
#include <vector>
using namespace std;

//Driver Code Ends

vector<int> middleSum(vector<vector<int>>& mat) {
    int n = mat.size();
    int rowSum = 0, colSum = 0;

    // Sum of middle row
    for (int i = 0; i < n; i++)
        rowSum += mat[n / 2][i];

    // Sum of middle column
    for (int i = 0; i < n; i++)
        colSum += mat[i][n / 2];

    return {rowSum, colSum };
}

//Driver Code Starts

int main() {
    vector<vector<int>> mat = {
        {2, 5, 7},
        {3, 7, 2},
        {5, 6, 9}
    };

    vector<int> result = middleSum(mat);
    cout <<result[0] << " " << result[1] << endl;
    return 0;
}

//Driver Code Ends
Java
//Driver Code Starts
import java.util.ArrayList;

class GFG {
//Driver Code Ends

    static ArrayList<Integer> middleSum(int[][] mat) {
        int n = mat.length;
        int rowSum = 0, colSum = 0;

        // Sum of middle row
        for (int i = 0; i < n; i++)
            rowSum += mat[n / 2][i];

        // Sum of middle column
        for (int i = 0; i < n; i++)
            colSum += mat[i][n / 2];

        ArrayList<Integer> result = new ArrayList<>();
        result.add(rowSum);
        result.add(colSum);

        return result;
    }

//Driver Code Starts

    public static void main(String[] args) {
        int[][] mat = {
            {2, 5, 7},
            {3, 7, 2},
            {5, 6, 9}
        };

        ArrayList<Integer> result = middleSum(mat);
        for(Integer element : result )
            System.out.print(element + " ");
    }
}

//Driver Code Ends
Python
def middleSum(mat):
    n = len(mat)
    rowSum = 0
    colSum = 0

    # Sum of middle row
    for i in range(n):
        rowSum += mat[n // 2][i]

    # Sum of middle column
    for i in range(n):
        colSum += mat[i][n // 2]

    return [rowSum, colSum]


#Driver Code Starts
if __name__ == "__main__":
    mat = [
        [2, 5, 7],
        [3, 7, 2],
        [5, 6, 9]
    ]

    result = middleSum(mat)
    print(*result)   

#Driver Code Ends
C#
//Driver Code Starts
using System;
using System.Collections.Generic;

class GFG { 
//Driver Code Ends

    static List<int> middleSum(int[,] mat) {
        int n = mat.GetLength(0);
        int rowSum = 0, colSum = 0;

        // sum of middle row
        for (int i = 0; i < n; i++)
            rowSum += mat[n / 2, i];

        // sum of middle column
        for (int i = 0; i < n; i++)
            colSum += mat[i, n / 2];

        return new List<int> { rowSum, colSum };
    }

//Driver Code Starts

    public static void Main()
    {
        int[,] mat = {
            { 2, 5, 7 },
            { 3, 7, 2 },
            { 5, 6, 9 }
        };

        List<int> result = middleSum(mat);
        Console.WriteLine($"{result[0]} {result[1]}");
    }
}

//Driver Code Ends
JavaScript
function middleSum(mat) {
    let n = mat.length;
    let rowSum = 0, colSum = 0;

    // sum of middle row
    for (let i = 0; i < n; i++)
        rowSum += mat[Math.floor(n / 2)][i];

    // sum of middle column
    for (let i = 0; i < n; i++)
        colSum += mat[i][Math.floor(n / 2)];

    return [rowSum, colSum];
}


//Driver Code Starts
// Driver code
let mat = [
    [2, 5, 7],
    [3, 7, 2],
    [5, 6, 9]
];

let result = middleSum(mat);
console.log(`${result[0]} ${result[1]}`); 

//Driver Code Ends

Output
12 18



Explore