Find the largest submatrix sum that does not exceed k

Quick Overview

Given an integer matrix and a limit k, find the largest sum of any rectangular block of cells that stays at or below k. It tests reducing a two-dimensional search to one dimension and handling a cap on subarray sums, where the usual maximum-subarray method no longer gives the answer.

Find the largest submatrix sum that does not exceed k

Company: Snapchat

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Other

Given an `m x n` integer matrix `matrix` and an integer `k`, return the largest sum of any rectangle in the matrix whose sum is no larger than `k`. It is guaranteed that at least one rectangle has a sum no larger than `k`. ### Function Signature ```python def max_sum_submatrix(matrix: list[list[int]], k: int) -> int: ``` ### Rules - A rectangle is a non-empty block of contiguous rows and contiguous columns, both ends inclusive. A single cell and the whole matrix are both rectangles. - The matrix is given as a list of rows, and every row has the same length. - Return only the sum, not the rectangle's position. ### Constraints - `m == len(matrix)` and `n == len(matrix[i])` - `1 <= m, n <= 100` - `-100 <= matrix[i][j] <= 100` - `-10^5 <= k <= 10^5` ### Examples ```text Input: matrix = [[1, 0, 1], [0, -2, 3]], k = 2 Output: 2 ``` No rectangle may go above 2, and two rectangles reach it exactly: the whole first row (`1 + 0 + 1`) and the 2 x 2 block in columns 1 to 2, `[[0, 1], [-2, 3]]` (`0 + 1 - 2 + 3`). ```text Input: matrix = [[2, 2, -1]], k = 3 Output: 3 ``` The first two cells sum to 4, which is over the limit. The whole row sums to `2 + 2 - 1 = 3`, which equals `k`. ```text Input: matrix = [[-3, 4], [2, -5]], k = -2 Output: -2 ``` The whole matrix sums to -2. The other eight rectangles sum to -3, 4, 2, -5, 1, -3, -1 and -1, so -2 is the largest sum that does not exceed `k`. The answer can be negative.

Overview: Given an integer matrix and a limit k, find the largest sum of any rectangular block of cells that stays at or below k. It tests reducing a two-dimensional search to one dimension and handling a cap on subarray sums, where the usual maximum-subarray method no longer gives the answer.

Snapchat logo
Snapchat
Nov 25, 2026
hardSoftware EngineerOtherCoding & Algorithms
0
0

Given an m x n integer matrix matrix and an integer k, return the largest sum of any rectangle in the matrix whose sum is no larger than k.

It is guaranteed that at least one rectangle has a sum no larger than k.

Function Signature

def max_sum_submatrix(matrix: list[list[int]], k: int) -> int:

Rules

  • A rectangle is a non-empty block of contiguous rows and contiguous columns, both ends inclusive. A single cell and the whole matrix are both rectangles.
  • The matrix is given as a list of rows, and every row has the same length.
  • Return only the sum, not the rectangle's position.

Constraints

  • m == len(matrix) and n == len(matrix[i])
  • 1 <= m, n <= 100
  • -100 <= matrix[i][j] <= 100
  • -10^5 <= k <= 10^5

Examples

Input: matrix = [[1, 0, 1], [0, -2, 3]], k = 2
Output: 2

No rectangle may go above 2, and two rectangles reach it exactly: the whole first row (1 + 0 + 1) and the 2 x 2 block in columns 1 to 2, [[0, 1], [-2, 3]] (0 + 1 - 2 + 3).

Input: matrix = [[2, 2, -1]], k = 3
Output: 3

The first two cells sum to 4, which is over the limit. The whole row sums to 2 + 2 - 1 = 3, which equals k.

Input: matrix = [[-3, 4], [2, -5]], k = -2
Output: -2

The whole matrix sums to -2. The other eight rectangles sum to -3, 4, 2, -5, 1, -3, -1 and -1, so -2 is the largest sum that does not exceed k. The answer can be negative.