Magicsheet logo

Count Submatrices with Top-Left Element and Sum Less Than k

Medium
100%
Updated 6/1/2025

Asked by 1 Company

Count Submatrices with Top-Left Element and Sum Less Than k

What is this problem about?

The Count Submatrices with Top-Left Element and Sum Less Than k coding problem involves a 2D grid of positive integers. Your objective is to find how many submatrices—specifically those that include the top-left corner of the original grid (index [0,0])—have a total sum that is strictly less than a given threshold kk.

Essentially, you are anchoring one corner of the submatrix at the origin and expanding the rectangle to different dimensions (r,c)(r, c). For every possible row index rr and column index cc, you calculate the sum of the elements within the rectangle defined by [0,0][0,0] to [r,c][r, c] and check if it meets the criteria.

Why is this asked in interviews?

Companies like Barclays ask this question to test a candidate's proficiency with 2D arrays and their ability to optimize calculations. While a naive approach might recalculate the sum for every possible submatrix, an efficient solution requires understanding how values accumulate in two dimensions. It demonstrates whether you can apply the prefix sum interview pattern to transform a potentially O(M2imesN2)O(M^2 imes N^2) or O(MimesNimesmin(M,N))O(M imes N imes \min(M,N)) problem into a clean O(MimesN)O(M imes N) linear scan.

Algorithmic pattern used

The primary pattern is the 2D Prefix Sum. In a 2D prefix sum array, each cell (i,j)(i, j) stores the sum of all elements in the rectangle from (0,0)(0, 0) to (i,j)(i, j). The formula to build this incrementally is: Sum(i, j) = grid(i, j) + Sum(i-1, j) + Sum(i, j-1) - Sum(i-1, j-1) Since we are only interested in submatrices starting at (0,0)(0, 0), we don't need the complex range-sum formula; we simply build the 2D prefix sum and count how many entries are less than kk.

Example explanation

Imagine a 2imes22 imes 2 grid:

1 2
3 4

And k=5k = 5.

  1. Submatrix (1x1): [1]. Sum = 1. (Valid, 1<51 < 5)
  2. Submatrix (1x2): [1, 2]. Sum = 3. (Valid, 3<53 < 5)
  3. Submatrix (2x1): [1], [3]. Sum = 4. (Valid, 4<54 < 5)
  4. Submatrix (2x2): [1, 2], [3, 4]. Sum = 10. (Invalid, 10510 \ge 5) Total count = 3.

Common mistakes candidates make

  • Inefficient Summation: Recalculating the sum for each rectangle from scratch, leading to O(M2N2)O(M^2 N^2) complexity.
  • Index Out of Bounds: Forgetting to handle the first row and first column separately (or padding the prefix sum array) when applying the cumulative formula.
  • Off-by-one errors: Misinterpreting "less than kk" as "less than or equal to kk".

Interview preparation tip

Mastering the 2D Prefix Sum is vital for matrix problems. Practice visualizing how the "overlap" (the top-left corner) is subtracted once because it was added twice by the top and left neighboring sums. This logic is the basis for many "Count Submatrices" or "Max Sum Subgrid" variations.

Similar Questions