Create another array to store the elements of matrix
let arr = new Array ( n * m ) ; arr . fill ( 0 ) ; let mod = matrix [ 0 ] [ 0 ] % k ; for ( let i = 0 ; i < n ; ++ i ) { for ( let j = 0 ; j < m ; ++ j ) { arr [ i * m + j ] = matrix [ i ] [ j ] ;