Sub-matrix Sums
Updated
Problem statement
Given an n x n matrix of positive integers and an integer threshold, find the largest size k such that every contiguous k x k sub-matrix has sum at most threshold.
Return the largest valid k. Return 0 if even size 1 is invalid.
Function
maxSize(matrix: int[][], threshold: int) → intExamples
Example 1
matrix = [[2, 2, 2], [3, 3, 3], [4, 4, 4]]threshold = 3return = 0The largest 1 x 1 sum is 4. Since this exceeds the threshold 3, no positive size is valid.
Example 2
matrix = [[2, 2, 2], [3, 3, 3], [4, 4, 4]]threshold = 4return = 1Every 1 x 1 sum is at most 4, but the largest 2 x 2 sum is 14. Therefore the largest valid size is 1.
Example 3
matrix = [[2, 2, 2], [3, 3, 3], [4, 4, 4]]threshold = 14return = 2Every 2 x 2 sum is at most 14, while the complete 3 x 3 matrix has sum 27. Therefore the answer is 2.
Example 4
matrix = [[2, 2, 2], [3, 3, 3], [4, 4, 4]]threshold = 27return = 3The complete 3 x 3 matrix has sum 27, so size 3 is valid.
Constraints
1 <= n <= 7501 <= threshold <= 10^91 <= matrix[i][j] <= 10^7- The sum of the entire matrix is at most
10^9.
More IMC Trading Online Assessment questions
- Earliest Meeting Start on a 24-Hour ClockOA · Seen Nov 2026
- Grid Paths with Override PassesOA · Seen Oct 2026
- Weighted LFU CacheOA · Seen Sep 2026
- Rock JumpingOA · Seen Aug 2026
- Stack Batch RemovalOA · Seen Aug 2026
- Chain of CommandOA · Seen Aug 2026
- Choose ContainersOA · Seen Aug 2026
- Sub-matrix SumsOA · Seen Jul 2026
- Can Every Package Fit in a BoxOA · Seen Jul 2026
- Minimum Space Wasted From PackagingOA · Seen Sep 2025
Similar questions at other companies
- Largest Sub-GridMathWorks · OAMathWorks interview questions
- Largest Square Sub-Grid Within a Sum LimitC3.ai · OAC3.ai interview questions
- Find Minimum Time to Create Beautiful CanvasAmazon · OA
- Largest Universally Bounded SubgridRoblox · OARoblox interview questions
- Subscription and Usage-Based Billing CalculatorStripe · OAStripe interview questions
- Count Lamps Lighting Each PointAirbnb · OAAirbnb interview questions
- Split a Matrix Into Four Partitions by Average (Forward Deployed Engineer)Boston Consulting Group · OABoston Consulting Group interview questions
- Leaves Remaining After Wind GustsOpenAI · OAOpenAI interview questions
- Shortest Path Around Rectangular ObstaclesDRW · OADRW interview questions
- Minimum Size Subarray SumMicrosoft · OAMicrosoft interview questions