27. Number of Submatrix have Sum X
β GFG solution to Number of Submatrix have Sum X: count square submatrices with specific sum using 2D prefix sum technique. π
π§© Problem Description
π Examples
Example 1
Input: mat[][] = [[2, 4, 7, 8, 10],
[3, 1, 1, 1, 1],
[9, 11, 1, 2, 1],
[12, -17, 1, 1, 1]], x = 10
Output: 3
Explanation: There are 3 square sub-matrices whose sum of elements equals 10.Example 2
Input: mat[][] = [[3, 3, 5, 3],
[2, 2, 2, 6],
[11, 2, 2, 4]], x = 1
Output: 0
Explanation: There is no square sub-matrix whose sum of elements is 1.Example 3
π Constraints
β
My Approach
2D Prefix Sum Technique
π Time and Auxiliary Space Complexity
π§βπ» Code (C++)
β Code (Java)
π Code (Python)
π§ Contribution and Support
πVisitor Count
Last updated