Generate Matrix B
Updated
Problem statement
You are given a 2D matrix A with m rows and n columns. Build a matrix B of the same size. Each element of B is the sum of all elements of A that are in the same or an earlier row and in the same or an earlier column.
In other words, B[i][j] is the sum of every A[r][c] with 0 ≤ r ≤ i and 0 ≤ c ≤ j.
Return the matrix B.
Function
generateMatrixB(A: int[][]) → int[][]Examples
Example 1
A = [[1,2,3],[4,5,6]]return = [[1,3,6],[5,12,21]]
The elements of matrix B are calculated as follows:
B(0,0) = A(0,0) = 1B(0,1) = A(0,0) + A(0,1) = 1 + 2 = 3B(0,2) = A(0,0) + A(0,1) + A(0,2) = 1 + 2 + 3 = 6B(1,0) = A(0,0) + A(1,0) = 1 + 4 = 5B(1,1) = A(0,0) + A(0,1) + A(1,0) + A(1,1) = 1 + 2 + 4 + 5 = 12B(1,2) = A(0,0) + A(0,1) + A(0,2) + A(1,0) + A(1,1) + A(1,2) = 1 + 2 + 3 + 4 + 5 + 6 = 21
Constraints
m == A.lengthn == A[i].lengthfor every rowi1 ≤ m, n ≤ 100-1000 ≤ A[i][j] ≤ 1000
More Arista Networks interview questions
- Generic Stack Operation SequencePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- First Missing Number in a Consecutive ArrayOA · PHONE SCREEN · Seen Oct 2026
- Remove All Repeated Values from a Linked ListPHONE SCREEN · Seen Oct 2026
- Trace Nested Object Destruction OrderPHONE SCREEN · Seen Oct 2026
- Trace Pointer Rebinding and Pointee UpdatesPHONE SCREEN · Seen Oct 2026
- API Bandwidth AllocationOA · Seen Sep 2026
- LFU CacheOA · Seen Sep 2026
- Maximum Server Processing TimeOA · Seen Aug 2026
- Minimum Adjacent Merges to Form a PalindromeOA · Seen Aug 2026
- Swap Adjacent NibblesPHONE SCREEN · Seen Aug 2026
Similar questions at other companies
- 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
- Sub-matrix SumsIMC Trading · OAIMC Trading interview questions
- Largest Sub-GridMathWorks · OAMathWorks interview questions
- Maximum Trailing Zeros on a Path with at Most One TurnMarshall Wace · OAMarshall Wace interview questions
- Find Minimum Time to Create Beautiful CanvasAmazon · OA
- Square-Neighborhood Image BlurZipRecruiter · OAZipRecruiter interview questions
- Count L-Shaped PlotsUber · OAUber interview questions
- Maximum CellsMicrosoft · OAMicrosoft interview questions