Problem ยท Matrix
Generate Matrix B
Learn this problemProblem statement
You are given a 2d matrix A of m rows and n columns and you have to generate a matrix B of same dimension but the elements must be a summation of all the previous elements up to now.
Function
generateMatrixB(A: int[][]) โ int[][]
Complete the function generateMatrixB in the editor.
generateMatrixB has the following parameter:
int[][] A: a 2D array of integers
Returns
int[][]: the generated matrix B
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
๐๐