Skip to content

Latest commit

 

History

History
67 lines (55 loc) · 1.54 KB

Print matrix in diagonal Pattern.md

File metadata and controls

67 lines (55 loc) · 1.54 KB
vector<int> matrixDiagonally(vector<vector<int>>&mat)
    {
         int r=0,c=0,i=0;
         int n = mat.size();
         bool up = true;
         vector<int> ans(n*n,0);
         
         while(r<n && c<n)
         {
             if(up)
             {
                 while(r>0 && c<n-1)
                 {
                     ans[i++] = mat[r][c];
                     r--;
                     c++;
                 }
                 
                 ans[i++] = mat[r][c];
                 
                 if(c==n-1)
                 {
                     r++;
                     //up = false;
                 }
                 else
                 {
                     c++;
                    // up = false;
                 }
             }
             
             else
             {
                 while(r<n-1 && c>0)
                 {
                     ans[i++] = mat[r][c];
                     c--;
                     r++;
                 }
                 
                 ans[i++] = mat[r][c];
                 
                 if(r==n-1)
                 {
                     c++;
                    // up = true;
                 }
                 
                 else
                {
                    r++;
                   // up = true;
                }
             }
             
             up = !up;
         }
         
         return ans;
    }