Problem · Matrix
Largest Sub-Grid
Learn this problemProblem statement
Given a square integer matrix grid and an integer maxSum, find the maximum side length k such that the sum of every k x k square subgrid is at most maxSum.
If no size satisfies this condition, return 0.
Function
largestSubGrid(grid: int[][], maxSum: int) → intExamples
Example 1
grid = [[2, 2, 2], [3, 3, 3], [4, 4, 4]]maxSum = 4return = 1Every 1 x 1 square has sum 2, 3, or 4, so size 1 is valid. A 2 x 2 square in the first two rows already has sum 10, so size 2 is invalid. The maximum valid side length is 1.
Constraints
gridis ann x nmatrix.1 <= n <= 3001 <= maxSum <= 10^9
More MathWorks problems
- Beautiful ArrangementONSITE INTERVIEW · Seen Jul 2026
- Find Minimum Cost to Remove Array ElementsONSITE INTERVIEW · Seen Jul 2026
- Group Shifted StringsONSITE INTERVIEW · Seen Jul 2026
- Longest Valid ParenthesesONSITE INTERVIEW · Seen Jul 2026
- Balancing TeamsOA · Seen Nov 2025
- Discount TagsOA · Seen Oct 2024
- Odd One OutOA · Seen Oct 2024
- Alloy ProductionOA · Seen Sep 2024