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
vector < vector < int > > ans ( n + m - 1 ) ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { ans [ i + j ] . push_back ( arr [ j ] [ i ] ) ; } } for ( int i = 0 ; i < ans . size ( ) ; i ++ ) { for ( int j = 0 ; j < ans [ i ] . size ( ) ; j ++ ) cout << ans [ i ] [ j ] << " ▁ " ; cout << endl ; } }