Print matrix elements from top | Function to traverse the matrix diagonally upwards ; Stores the maximum size of vector from all row of matrix nums [ ] [ ] ; Store elements in desired order ; Store every element on the basis of sum of index ( i + j ) ; Print the stored result ; Print in reverse order ; Given vector of vectors arr ; Function Call
function printDiagonalTraversal ( nums ) { let max_size = nums [ 0 ] . length ; let v = [ ] ; for ( let i = 0 ; i < 2 * max_size - 1 ; i ++ ) { v . push ( [ ] ) ; } for ( let i = 0 ; i < nums [ 0 ] . length ; i ++ ) { for ( let j = 0 ; j < nums [ 0 ] . length ; j ++ ) { v [ i + j ] . push ( nums [ i ] [ j ] ) ; } } for ( let i = 0 ; i < v . length ; i ++ ) { for ( let j = v [ i ] . length - 1 ; j >= 0 ; j -- ) { document . write ( v [ i ] [ j ] + " " ) ; } } } let arr = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] ; printDiagonalTraversal ( arr ) ;