Skip to content

Latest commit

 

History

History
46 lines (34 loc) · 1.03 KB

25.md

File metadata and controls

46 lines (34 loc) · 1.03 KB
 vector<int> antiDiagonalPattern(vector<vector<int>> matrix) 
    {
       
        vector<int> v;
        int n=matrix.size();
        int m=matrix[0].size();
        
        v.push_back(matrix[0][0]);
        
        if(n==1)
            return v;
        
        int i=0,j=1;
        
        while(i<n || j<m)
        {
           
            v.push_back(matrix[i][j]);
            int t=i,f=j;
            
            while(t<n && f>=0)
            {
                t=t+1;          //incresing row
                f=f-1;          //decreasing col
                 if (t <n && f >=0) // Ensure indices are within bounds
                v.push_back(matrix[t][f]);
            }
            
            if(j!=m-1)
                j++;
            
            else if(i!=n-1)
            {
                i++;
            }
            else
                break;
        }
       
        
        return v;
    }