Print matrix elements from top | C # program for the above approach ; 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 ; Driver Code ; Given vector of vectors arr ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void printDiagonalTraversal ( int [ , ] nums ) { int max_size = nums . GetLength ( 0 ) ; List < List < int > > v = new List < List < int > > ( ) ; for ( int i = 0 ; i < 2 * max_size - 1 ; i ++ ) { v . Add ( new List < int > ( ) ) ; } for ( int i = 0 ; i < nums . GetLength ( 0 ) ; i ++ ) { for ( int j = 0 ; j < nums . GetLength ( 0 ) ; j ++ ) { v [ i + j ] . Add ( nums [ i , j ] ) ; } } for ( int i = 0 ; i < v . Count ; i ++ ) { for ( int j = v [ i ] . Count - 1 ; j >= 0 ; j -- ) { Console . Write ( v [ i ] [ j ] + " ▁ " ) ; } } } static void Main ( ) { int [ , ] arr = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; printDiagonalTraversal ( arr ) ; } }