Print Diagonally | Practice | GeeksforGeeks

Give a N * N square matrix, return all the elements of its anti-diagonals from top to bottom. 
Example 1:
Input: 
N = 2
A = [[1, 2],
     [3, 4]]
Output:
1 2 3 4
Explanation: Topmost anti-diagonal is [[1, ], 
                                       [ , ]]
Next anti-diagonal is [[ , 2], 
â€‹                       [3,  ]]
and the last anti-diagonal is [[ ,  ], 
â€‹                               [ , 4]]
Hence, elements will be returned in the 
order {1, 2, 3, 4}.

Example 2:
Input: 
N = 3 
A = [[1, 2, 3],
     [4, 5, 6],
     [7, 8, 9]]
Output: 
1 2 4 3 5 7 6 8 9

Your Task:
You don't need to read input or print anything. Your task is to complete the function downwardDigonal() which takes an integer N and a 2D matrix A[ ][ ] as input parameters and returns the list of all elements of its anti-diagonals from top to bottom.
Expected Time Complexity: O(N*N)
Expected Auxillary Space: O(N*N)
Constraints:
1 ≤ N, M ≤ 103
0 ≤ A[i][j] ≤ 106
0
priyansh708902 days ago
  vector<int>v;    for(int i=0;i<n;i++){  int row=0,col=i;  while(col>=0){  v.push_back(A[row][col]);row++;col--;  }   }     for(int i=1;i<n;i++){       int row=i,col=n-1;       while(row<n){           v.push_back(A[row][col]);           row++;           col--;       }   }   return v;}
0
roy99mustang4 days ago
Easy python solution : 
#User function Template for python3

def downwardDigonal(n, A): 
    c = []
    for l in range(2*n-1):
        t, b = max(0, l+1-n), min(l, n-1)
        
        for i in range(t, b+1):
            c.append(A[i][l-i])
    
    return c
    
    
#{ 
#  Driver Code Starts
#Initial Template for Python 3

if __name__ == '__main__':
    t = int(input())
    for _ in range(t):
        n = int(input())
        matrix =[]
        for i in range(n):
            row = list(map(int, input().strip().split()))
            matrix.append(row)
        ans = downwardDigonal(n,matrix)
        for i in ans:
            print(i,end=" ")
        print()

# } Driver Code Ends
+1
sharma9897akash1 week ago
 
SINGLE LOOP O(n) SOLUTION
 
ArrayList<Integer> al=new ArrayList<>();       int count=0,st=0,en=0;       for(int i=0;i<n*(n+1)/2;i++)       {           al.add(A[st][en]);           if(en==0)           {               en=st+1;               st=0;                          }           else           {               st+=1;               en-=1;           }       }       st=1;en=n-1;       int c;       for(int i=0;i<n*(n-1)/2;i++)       {           al.add(A[st][en]);           if(st==n-1)           {               c=en;               en=st;               st=c+1;                          }           else           {               st+=1;               en-=1;           }       }       return al;
0
harshsinghreal2 weeks ago
MY C++ Approach
vector<int> downwardDigonal(int n, vector<vector<int>> A)
	{
	    vector<int> v;
		for(int k=0;k<n;k++){
		    int i = 0,j=k;
		      
		      while(j>=0){
		        v.push_back(A[i][j]);
		        i++; j--;
		    }
		}
		
		
		for(int k=1;k<n;k++){
		    int i = k, j = n-1;
		     
		     while(i<n){
		        v.push_back(A[i][j]);
		        j--;
		        i++;
		    }
		}
		
		return v;
	}
0
amansidd178613 weeks ago
vector<int> downwardDigonal(int n, vector<vector<int>> arr){ // Your code goes here vector<int> ans; int row=0; int i=0; while(row<n){  int r = row;  int c = i;  while(r<n and c>=0){      ans.push_back(arr[r][c]);      r++,c--;  }  if(i==n-1) row++;  else i++; } return ans;}
0
kmehta33 weeks ago
Python Naïve implementation:
 
def downwardDigonal(n, A):    # code here    output = []   for col in range(n):       j = col       row = 0       while j >= 0:           element = A[row][j]           output.append(element)           row += 1           j -= 1   for row in range(1, n):       i = row       col = n - 1       while i < n:           element = A[i][col]           output.append(element)           col -= 1           i += 1   return output   
0
ajay7yadav953 weeks ago
//hi JavaScript lovers -----------→ A7
let arr = [[1, 2],
          [3, 4]];
let str = "";
for(let i=0;i<arr.length;i++){
    for(let j=0;j<arr[0].length;j++){
        str += arr[i][j]+" ";
    }
}
return str;
0
bajpayeevinayak2281 month ago
 
 
reader...keep faith in your self...you are the best...and you only can help your self...
// { Driver Code Starts#include<bits/stdc++.h>using namespace std;
// } Driver Code Ends
class Solution{public:vector<int> downwardDigonal(int n, vector<vector<int>> A){ // Your code goes here vector<int> ans; ///reader you need to dry run the code have faith in your self;;; for(int i=0;i<n;i++) {     int j=i,k=0;     for(;j>=0;j--)     {         ans.push_back(A[k][j]);         k++;     } } for(int i=1;i<n;i++) {     int j=i,k=n-1;     for(;j<n;j++)     {         ans.push_back(A[j][k]);         k--;     } } return ans;}
};
// { Driver Code Starts.
 
int main(){
      int t;   cin >> t;   while(t--)    {       int n;       cin >> n;
       vector<vector<int>> A(n, vector<int>(n));
       for(int i = 0; i < n; i++)        for(int j = 0; j < n; j++)         cin >> A[i][j];
       Solution obj;       vector<int> ans = obj.downwardDigonal(n, A);
       for(auto i:ans)        cout << i << " ";
    cout << "\n";   }
   return 0;} // } Driver Code Ends
+1
durpb12011 month ago
C++ code
	vector<int> res;
		int k= 0,l=1;
		while(k<(A[0].size()+A.size()-1)){
		    if(k<A[0].size()){
		        int i = 0;
		        for(int j = k;j >=0;j--)
		        {
		            res.push_back(A[i][j]);
		            i++;
		        }
		        k++;
		    }
		    else
		    {
		       int j = A[0].size()-1;
		       for(int i = l ; i < A.size();i++)
		       {
		           res.push_back(A[i][j]);
		           j--;
		       }
		       l++;
		       k++;
		    }
		}
		return res;
0
abhisinha6562 months ago
vector<int> downwardDigonal(int n, vector<vector<int>> A){ vector<int>V;       for(int i=0; i<n; i++){           int k=0, j=i;           for(; j>=0; j--){               V.push_back(A[k][j]);               k++;           }       }       for(int i=1; i<n; i++){           int k = n-1, j=i;           for(; j<n; j++){               V.push_back(A[j][k]);               k--;           }       }       return V;}
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.