Maximum k-by-k Square Sum
Updated
Problem statement
You are given a square integer matrix mat and an integer k. Return the greatest sum of any contiguous k x k square inside the matrix.
For this exercise, return the sum as one integer. A square uses k consecutive rows and k consecutive columns and must fit entirely inside the matrix. Negative entries are allowed, and you must choose a square even when every possible sum is negative.
Function
maximumSquareSum(mat: int[][], k: int) → intExamples
Example 1
mat = [[1,2,3],[4,5,6],[7,8,9]]k = 2return = 28The bottom-right 2-by-2 square contains 5,6,8,9 and sums to 28. The other square sums are 12,16 and 24.
Example 2
mat = [[-5,-2],[-3,-4]]k = 1return = -2A 1-by-1 square is one cell. The greatest cell value is -2; choosing an empty square is not allowed.
Constraints
1 <= mat.length <= 50.1 <= mat[i].length <= 50, and every row has exactly mat.length entries.-10000 <= mat[i][j] <= 10000.1 <= k <= mat.length.
More JP Morgan Chase Online Assessment questions
- Find Total CostOA · Seen Oct 2026
- Minimum Meeting RoomsOA · Seen Oct 2026
- Count Dropped RequestsOA · Seen Oct 2026
- Maximize Minimum Deployment DifficultyOA · Seen Oct 2026
- Task SchedulingOA · Seen Oct 2026
- Substring RemovalOA · Seen Oct 2026
- Maximum Game Board ScoreOA · Seen Sep 2026
- Minimum Digit Changes Between String HalvesOA · Seen Sep 2026
- Same Substring Within BudgetOA · Seen Sep 2026
- Invert Ad Visibility BitsOA · Seen Aug 2026
Similar questions at other companies
- Most Visited Sprint MarkerMicrosoft · OAMicrosoft interview questions
- Earliest Time to Schedule a MeetingMathWorks · OAMathWorks interview questions
- Earliest Meeting Start on a 24-Hour ClockIMC Trading · OAIMC Trading interview questions
- Calculate the Sum After Range TogglesBlink Health · OA
- Optimal TransferSnowflake · OASnowflake interview questions
- Count Attack Vectors by Divisor RangeVisa · OAVisa interview questions
- Smallest Most-Lit Street PositionHudson River Trading · OAHudson River Trading interview questions
- Most Visited Sprint MarkerChubb · OAChubb interview questions
- Minimum Seats for One-Way Car PoolingSquadStack.ai · OA
- Shortest Path Around Rectangular ObstaclesDRW · OADRW interview questions
Interview reports that mention this question
JP Morgan Chase interview experiences
- JPMorgan Quant Stats Superdays: 3 Experiences (Part 3 of 3)
- JPMorgan Quant Modeling Superdays: 7 Experiences (Part 2 of 3)
- JPMorgan Quant Intern Superday: 19 Experiences (Part 1 of 3)
- JP Morgan Chase Data and AI Intern Superday: 11 Experiences
- JPMorgan Code for Good India 2024: 6 Experiences (Part 5 of 5)