Very easy solution by dividing the Array diagonally. Understandable for beginners


#1

vector<vector > Solution::diagonal(vector<vector > &A) {
vector<vector> dia;
int n= A.size();
for(int i=0;i<n;i++){
vector temp;
for(int j=i,k=0; j>=0&&k<=i; j–,k++){
temp.push_back(A[k][j]);
}
dia.push_back(temp);
}
for(int k=1;k<n;k++){
vectortemp;
for(int i=k,j=n-1;i<n&&j>=k;i++,j–){
temp.push_back(A[i][j]);
}
dia.push_back(temp);
}

return dia;

}