Problem · Matrix

Largest Square Subgrid Under a Sum Limit

Learn this problem
MediumOpenAIOA

Problem statement

You are given a two-dimensional integer array grid and an integer maxSum.

For a side length k, consider every contiguous k x k square subgrid. The side length is valid when the sum of every such square is at most maxSum.

Return the maximum valid side length.

Source note: The report clearly described the array, the sum limit, and the requirement that every square of the chosen size satisfy the limit. This version is a close reconstruction of that core task; the original example, function signature, and numeric limits were not included.

Function

largestSquareSubgrid(grid: int[][], maxSum: int) → int

Examples

Example 1

grid = [[1,1,1],[1,1,1],[1,1,1]]maxSum = 4return = 2

Every 2 x 2 square has sum 4, while the only 3 x 3 square has sum 9. Therefore the largest valid side length is 2.

More OpenAI problems

drafts saved locally
public int largestSquareSubgrid(int[][] grid, int maxSum) {
  // write your code here
}
grid[[1,1,1],[1,1,1],[1,1,1]]
maxSum4
expected2
checking account