Zigzag ( or diagonal ) traversal of Matrix |  ; we will use a 2D vector to store the diagonals of our array the 2D vector will have ( n + m - 1 ) rows that is equal to the number of diagnols ; Driver code ; Function call
using System ; using System . Collections . Generic ; public class GFG { public static int R = 5 , C = 4 ; public static void diagonalOrder ( int [ , ] arr , int n , int m ) { List < List < int > > ans = new List < List < int > > ( n + m - 1 ) ; for ( int i = 0 ; i < n + m - 1 ; i ++ ) { ans . Add ( new List < int > ( ) ) ; } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { ( ans [ i + j ] ) . Add ( arr [ i , j ] ) ; } } for ( int i = 0 ; i < ans . Count ; i ++ ) { for ( int j = ans [ i ] . Count - 1 ; j >= 0 ; j -- ) { Console . Write ( ans [ i ] [ j ] + " ▁ " ) ; } Console . WriteLine ( ) ; } } static public void Main ( ) { int n = 5 , m = 4 ; int [ , ] arr = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } , { 17 , 18 , 19 , 20 } , } ; diagonalOrder ( arr , n , m ) ; } }