Anti Diagonal Traversal of Matrix | Practice | GeeksforGeeks

Give a N*N square matrix, return an array of its anti-diagonals. Look at the example for more details.
Example 1:
Input:
N = 2
matrix[][] = {{1,2},
              {3,4}}
Output:
1 2 3 4
Explanation:
Matrix is as below:
1 2
3 4
Printing it in anti-diagonal form will lead
to the output as 1 2 3 4

Example 2:
Input:
N = 3
matrix[][] = {{1,2,3},

              {4,5,6},

              {7,8,9}}
Output:
1 2 4 3 5 7 6 8 9
Explanation: 
Matrix is as below:
1 2 3
4 5 6
7 8 9
Printing it in anti-diagonal form will lead
to the output as 1 2 4 3 5 7 6 8 9 
Your Task:
You dont need to read input or print anything. Complete the function antiDiagonalPattern() that takes matrix as input parameter and returns a list of integers in order of the values visited in the anti-Diagonal pattern. 
Expected Time Complexity: O(N * N)
Expected Auxiliary Space: O(N * N) for the resultant list only.
 
Constraints:
1 <= N <= 100
1 <= mat[i][j] <= 100
0
anilcangulkaya72 weeks ago
// O(1) 0.03/1.6
vector<int> antiDiagonalPattern(vector<vector<int>> matrix) 
{
   // Code here
   int submisionIndex = 0; // submition
   vector<int> result(matrix.size() * matrix[0].size());    
   
   for (int i = 0; i < matrix[0].size(); ++i)
   {
       result[submisionIndex++] = matrix[0][i];
       for (int j = i-1, k = 1; j >= 0; --j, k++)
       {
           result[submisionIndex++] = matrix[k][j];    
       }
   }
   
   const int lastMatrixIndex = matrix.size()-1;
   submisionIndex = result.size()-1;
   
   for (int i = matrix[lastMatrixIndex].size()-1; i > 0; --i)
   {
       result[submisionIndex--] = matrix[lastMatrixIndex][i];
       for (int j = i+1, k = lastMatrixIndex-1; j < matrix.size(); ++j, --k)
       {
           result[submisionIndex--] = matrix[k][j];    
       }
   }
   
   return result;
}
+1
himanshuj701 month ago
//Easy C++ code
vector<int> antiDiagonalPattern(vector<vector<int>> matrix) 
    {
        // Code here
        int n=matrix.size();
        vector<int> vec;
        vec.push_back(matrix[0][0]);
        if(n==1){
            return vec;
        }
        int i=0, j=1;
        while(j<n){
            int x=i, y=j;
            while(x!=j && y!=i){
                vec.push_back(matrix[x][y]);
                x++;
                y--;
            }
            vec.push_back(matrix[x][y]);
            j++;
        }
        i=1;
        j=n-1;
        while(i<n){
            int x=i, y=j;
            while(x!=j && y!=i){
                vec.push_back(matrix[x][y]);
                x++;
                y--;
            }
            vec.push_back(matrix[x][y]);
            i++;
        }
        return vec;
    }
0
honeychopra061 month ago
Easy python solution
class Solution:   def antiDiagonalPattern(self,matrix):       # Code here       n=len(matrix)       l=[]       for i in range(n):           a=0           b=i           while(a<=i and b>=0):               l.append(matrix[a][b])               a=a+1               b=b-1       for j in range(1,n):           a=j           b=n-1           while(a<=n and b>=j):               l.append(matrix[a][b])               a=a+1               b=b-1                  return l
0
rajkumarchauhan844784 months ago
// c++ easy solution    
vector<int> antiDiagonalPattern(vector<vector<int>> matrix)    {       // Code here       vector<int>ans;       int n=matrix.size();       for(int i=0;i<n;i++)       {       // {   if(i>=n)       //      i=n-1;           int k=i;           for(int j=0;j<=i;j++)           {               if(k>=0 && j<n )               ans.push_back(matrix[j][k]);               k--;           }       }       for(int i=0;i<n-1;i++)       {       // {   if(i>=n)       //      i=n-1;           int k=n-1;           for(int j=i+1;j<n;j++)           {               if(k>=0 && j<n )               ans.push_back(matrix[j][k]);               k--;           }       }       return ans;   }}; 
0
imranwahid5 months ago
Easy C++ solution
0
anutiger7 months ago
    int helper(vector<vector<int>> &mat,int x,int y){
        if(x >= 0  and x < mat.size() and y >= 0 and y < mat[0].size() and mat[x][y] != -1){
            int t = mat[x][y];
            mat[x][y] = -1;
            return t;
        }
        return -1;
    }
    vector<int> antiDiagonalPattern(vector<vector<int>> mat) 
    {
        queue<pair<pair<int,int>,int>> q;
        vector<int> res;
        q.push({{0,0},mat[0][0]});
        mat[0][0] = -1;
        int dx[] = {0,0,-1,1};
        int dy[] = {1,-1,0,0};
        while(!q.empty()){
            auto it = q.front();q.pop();
            int x = it.first.first;
            int y = it.first.second;
            res.push_back(it.second);
            for(int i = 0 ;i < 4 ; i++){
                int t = helper(mat,x + dx[i],y + dy[i]);
                if(t!= -1) q.push({{x + dx[i],y + dy[i]},t});
            }
        }
        return res;
    }
0
Suhaas Badada9 months ago
Suhaas Badada
https://pastebin.com/41mqNsB0
0
Tanay Gupta10 months ago
Tanay Gupta
If admin corrects the test cases for python:-
Below code will work fine:
matrix = [[1,2,3],[4,5,6],[7,8,9]]n = len(matrix)
m = len(matrix[0])
ans = [[] for i in range(n + m - 1)]
for i in range(m):    for j in range(n):        ans[i + j].append(matrix[i][j])ask =[]for i in range(len(ans)):    for j in range(len(ans[i])):        ask.append(ans[i][j])
return ask
0
Tanay Gupta10 months ago
Tanay Gupta
For python, Please correct the base test case.
For Input:31 2 3 4 5 6 7 8 9your output is: [] [1, 2, 3] [4, 5, 6]
Please correct all the test cases
0
Debojyoti Sinha10 months ago
Debojyoti Sinha
Correct Answer. ✅Execution Time:0.05
class Solution{    public:    vector<int> antiDiagonalPattern(vector<vector<int>> matrix)     {        map<int, vector<int="">> mp;                for(int i = 0; i < matrix.size(); i++)        {            for(int j = 0; j < matrix[i].size(); j++)            {                mp[i + j].push_back(matrix[i][j]);            }        }                vector<int> res;                for(auto x: mp)        {            res.insert(res.end(), x.second.begin(), x.second.end());        }                return res;    }};
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.