Sum of Submatrix Elements

Last Updated : 21 Sep, 2026

Given a matrixĀ arr[][]Ā of sizeĀ n * mĀ and four integersĀ x1,Ā y1,Ā x2, andĀ y2Ā representing the top-left and bottom-right corners of a submatrix (using 1-based indexing), find the sum of all elements in that submatrix.

Examples:

Input: arr[][] = [[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12], [13, 14, 15, 16, 17, 18], [19, 20, 21, 22, 23, 24], [25, 26, 27, 28, 29, 30]], x1 = 3, y1 = 4, x2 = 4, y2 = 5
Output: 78
Explanation: Sum from cell starting at position (3, 4) (1-based indexing) and ending at (4, 5) is 78 (16 + 17 + 22 + 23).

Input: arr[][] = [[9, 8, 7], [4, 2, 1], [6, 5, 3]], x1 = 1, y1 = 2, x2 = 3, y2 = 3
Output: 26
Explanation: Sum from cell starting at position (1, 2) (1-based indexing) and ending at (3, 3) is 26.

Try It Yourself
redirect icon

Direct Traversal - O((x2 - x1 + 1) * (y2 - y1 + 1)) Time and O(1) Space

The idea is to directly traverse all the cells inside the given submatrix (from x1 - 1 to x2 - 1 and y1 - 1 to y2 - 1) and add their values to the result.

Working of Approach:

  • Convert the given 1-based row and column indices to 0-based indices while traversing.
  • Start from row x1 - 1 and column y1 - 1.
  • Traverse all rows up to x2 - 1 and columns up to y2 - 1.
  • Add every element inside the submatrix to res.
  • Return res as the required sum.
C++
#include <iostream>
#include <vector>
using namespace std;

int subMatrixSum(vector<vector<int>> &arr, int x1, int y1, int x2, int y2)
{

    int res = 0;

    // Traverse every cell inside the given submatrix.
    for (int i = x1 - 1; i < x2; i++)
    {
        for (int j = y1 - 1; j < y2; j++)
        {
            res += arr[i][j];
        }
    }

    return res;
}

int main()
{

    vector<vector<int>> arr = {{1, 2, 3, 4, 5, 6},
                               {7, 8, 9, 10, 11, 12},
                               {13, 14, 15, 16, 17, 18},
                               {19, 20, 21, 22, 23, 24},
                               {25, 26, 27, 28, 29, 30}};

    int x1 = 3, y1 = 4;
    int x2 = 4, y2 = 5;

    cout << subMatrixSum(arr, x1, y1, x2, y2) << endl;

    return 0;
}
Java
import java.util.Arrays;
import java.util.List;

public class GFG {

    public static int subMatrixSum(int[][] arr, int x1,
                                   int y1, int x2, int y2)
    {

        int res = 0;

        // Traverse every cell inside the given submatrix.
        for (int i = x1 - 1; i < x2; i++) {
            for (int j = y1 - 1; j < y2; j++) {
                res += arr[i][j];
            }
        }

        return res;
    }

    public static void main(String[] args)
    {

        int[][] arr = { { 1, 2, 3, 4, 5, 6 },
                        { 7, 8, 9, 10, 11, 12 },
                        { 13, 14, 15, 16, 17, 18 },
                        { 19, 20, 21, 22, 23, 24 },
                        { 25, 26, 27, 28, 29, 30 } };

        int x1 = 3, y1 = 4;
        int x2 = 4, y2 = 5;

        System.out.println(
            subMatrixSum(arr, x1, y1, x2, y2));
    }
}
Python
def subMatrixSum(arr, x1, y1, x2, y2):

    res = 0

    # Traverse every cell inside the given submatrix.
    for i in range(x1 - 1, x2):
        for j in range(y1 - 1, y2):
            res += arr[i][j]

    return res

if __name__ == '__main__':

    arr = [[1, 2, 3, 4, 5, 6],
           [7, 8, 9, 10, 11, 12],
           [13, 14, 15, 16, 17, 18],
           [19, 20, 21, 22, 23, 24],
           [25, 26, 27, 28, 29, 30]]

    x1 = 3; y1 = 4
    x2 = 4; y2 = 5

    print(subMatrixSum(arr, x1, y1, x2, y2))
C#
using System;

class GFG {

    static int subMatrixSum(int[][] arr, int x1, int y1,
                            int x2, int y2)
    {
        int res = 0;

        // Traverse every cell inside the given submatrix.
        for (int i = x1 - 1; i < x2; i++) {
            for (int j = y1 - 1; j < y2; j++) {
                res += arr[i][j];
            }
        }

        return res;
    }

    static void Main()
    {
        int[][] arr
            = { new int[] { 1, 2, 3, 4, 5, 6 },
                new int[] { 7, 8, 9, 10, 11, 12 },
                new int[] { 13, 14, 15, 16, 17, 18 },
                new int[] { 19, 20, 21, 22, 23, 24 },
                new int[] { 25, 26, 27, 28, 29, 30 } };

        int x1 = 3, y1 = 4;
        int x2 = 4, y2 = 5;

        Console.WriteLine(
            subMatrixSum(arr, x1, y1, x2, y2));
    }
}
JavaScript
function subMatrixSum(arr, x1, y1, x2, y2)
{

    let res = 0;

    // Traverse every cell inside the given submatrix.
    for (let i = x1 - 1; i < x2; i++) {
        for (let j = y1 - 1; j < y2; j++) {
            res += arr[i][j];
        }
    }

    return res;
}

// Driver Code
const arr = [
    [ 1, 2, 3, 4, 5, 6 ], [ 7, 8, 9, 10, 11, 12 ],
    [ 13, 14, 15, 16, 17, 18 ], [ 19, 20, 21, 22, 23, 24 ],
    [ 25, 26, 27, 28, 29, 30 ]
];

let x1 = 3, y1 = 4;
let x2 = 4, y2 = 5;

console.log(subMatrixSum(arr, x1, y1, x2, y2));

Output
78
Comment