Description
Solutions
Generate Matrix B

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 Description

Complete the function generateMatrixB in the editor.

generateMatrixB has the following parameter:

  1. int[][] A: a 2D array of integers

Returns

int[][]: the generated matrix B

Example 1:

Input:  A = [[1,2,3],[4,5,6]]
Output: [[1,3,6],[5,12,21]]
Explanation:

The elements of matrix B are calculated as follows:

  • B(0,0) = A(0,0) = 1
  • B(0,1) = A(0,0) + A(0,1) = 1 + 2 = 3
  • B(0,2) = A(0,0) + A(0,1) + A(0,2) = 1 + 2 + 3 = 6
  • B(1,0) = A(0,0) + A(1,0) = 1 + 4 = 5
  • B(1,1) = A(0,0) + A(0,1) + A(1,0) + A(1,1) = 1 + 2 + 4 + 5 = 12
  • B(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:
    ๐Ÿ‰๐Ÿ‰
Thumbnail 0
Testcase

Result
Case 1

input:

output: