Print Diagonally-misc

 Give a N*N square matrix, return an array of its anti-diagonals.

For Example:
If the matrix is    

1 2 3
4 5 6
7 8 9
The output should Return the following :


  [1],
  [2, 4],
  [3, 5, 7],
  [6, 8],
  [9]
]
i.e print the elements of array diagonally.


c++ implementation:

t is the no of test cases:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int

int main() {
	int t;
	cin>>t;
	while(t--)
	{
	    ll n;
	    cin>>n;
	    ll arr[n][n];
	    ll i,j;
	    map<ll,vector<ll>>m;
	    for(i=0;i<n;i++)
	    {
	        for(j=0;j<n;j++)
	        {
                cin>>arr[i][j];
	            m[i+j].push_back(arr[i][j]);
	        }
	    }
	    for(auto itr=m.begin();itr!=m.end();itr++)
	    {
	        for(auto it=itr->second.begin();it!=itr->second.end();it++)
	        {
	            cout<<*it<<" ";
	        }
	    
	    }
        cout<<endl;
	}
	return 0;
}
darkmode