Problem ยท Matrix

Generate Matrix B

Learn this problem
โ— EasyArista NetworksOA

Problem 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:

  1. 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) = 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

๐Ÿ‰๐Ÿ‰
drafts saved locally
public int[][] generateMatrixB(int[][] A) {
  // write your code here
}
A[[1,2,3],[4,5,6]]
expected[[1,3,6],[5,12,21]]
checking account