Return an array of anti-diagonals of given N*N square matrix

Given a square matrix of size N*N, return an array of its anti-diagonals. For better understanding let us look at the image given below:

Examples:

Input :
null

Output :
 1
 2  5
 3  6  9
 4  7  10  13
 8  11 14
 12 15
 16

Approach:

To solve the problem mentioned above we have two major observations.

  • The first one is, some diagonals start from the zeroth row for each column and ends when either start column >= 0 or start row < N.
  • While the second observation is that the remaining diagonals start with end column for each row and ends when either start row < N or start column >= 0.

Below is the implementation of the above approach:

C++



filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP implementation to  return
// an array of its anti-diagonals
// when an N*N square matrix is given
  
#include <iostream>
using namespace std;
  
// function to print the diagonals
void diagonal(int A[3][3])
{
  
    int N = 3;
  
    // For each column start row is 0
    for (int col = 0; col < N; col++) {
  
        int startcol = col, startrow = 0;
  
        while (startcol >= 0 && startrow < N) {
            cout << A[startrow][startcol] << " ";
  
            startcol--;
  
            startrow++;
        }
        cout << "\n";
    }
  
    // For each row start column is N-1
    for (int row = 1; row < N; row++) {
        int startrow = row, startcol = N - 1;
  
        while (startrow < N && startcol >= 0) {
            cout << A[startrow][startcol] << " ";
  
            startcol--;
  
            startrow++;
        }
        cout << "\n";
    }
}
  
// Driver code
int main()
{
  
    // matrix iniliasation
    int A[3][3] = { { 1, 2, 3 },
                    { 4, 5, 6 },
                    { 7, 8, 9 } };
  
    diagonal(A);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// JAVA implementation to  return
// an array of its anti-diagonals
// when an N*N square matrix is given
  
class Matrix {
  
    // function to print the diagonals
    void diagonal(int A[][])
    {
  
        int N = 3;
  
        // For each column start row is 0
        for (int col = 0; col < N; col++) {
  
            int startcol = col, startrow = 0;
  
            while (startcol >= 0 && startrow < N) {
  
                System.out.print(A[startrow][startcol] + " ");
  
                startcol--;
  
                startrow++;
            }
            System.out.println();
        }
  
        // For each row start column is N-1
        for (int row = 1; row < N; row++) {
            int startrow = row, startcol = N - 1;
  
            while (startrow < N && startcol >= 0) {
                System.out.print(A[startrow][startcol] + " ");
  
                startcol--;
  
                startrow++;
            }
            System.out.println();
        }
    }
  
    // Driver code
    public static void main(String args[])
    {
  
        // matrix initialisation
        int A[][] = { { 1, 2, 3 },
                      { 4, 5, 6 },
                      { 7, 8, 9 } };
  
        Matrix m = new Matrix();
  
        m.diagonal(A);
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to return
# an array of its anti-diagonals
# when an N*N square matrix is given
  
# function to print the diagonals
def diagonal(A) :
  
    N = 3;
  
    # For each column start row is 0
    for col in range(N) :
  
        startcol = col; startrow = 0;
  
        while(startcol >= 0 and
              startrow < N) :
            print(A[startrow][startcol], 
                             end = " ");
  
            startcol -= 1;
            startrow += 1;
      
        print()
  
    # For each row start column is N-1
    for row in range(1, N) :
        startrow = row; startcol = N - 1;
  
        while(startrow < N and 
              startcol >= 0) :
            print(A[startrow][startcol],
                             end = " ");
  
            startcol -= 1;
            startrow += 1;
          
        print()
  
# Driver code
if __name__ == "__main__" :
  
    # matrix iniliasation
    A = [ [ 1, 2, 3 ],
          [ 4, 5, 6 ],
          [ 7, 8, 9 ] ];
  
    diagonal(A);
  
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to return 
// an array of its anti-diagonals 
// when an N*N square matrix is given 
using System;
  
class GFG{ 
  
// Function to print the diagonals 
static void diagonal(int [,]A) 
    int N = 3; 
  
    // For each column start row is 0 
    for(int col = 0; col < N; col++)
    
       int startcol = col, startrow = 0; 
         
       while (startcol >= 0 && startrow < N)
       
           Console.Write(A[startrow, startcol] + " "); 
           startcol--; 
           startrow++; 
       
       Console.WriteLine(); 
    }
      
    // For each row start column is N-1 
    for(int row = 1; row < N; row++)
    
       int startrow = row, startcol = N - 1; 
         
       while (startrow < N && startcol >= 0)
       
           Console.Write(A[startrow, startcol] + " "); 
           startcol--; 
           startrow++; 
       
       Console.WriteLine(); 
    
  
// Driver code 
public static void Main(string []args) 
  
    // Matrix initialisation 
    int [,]A = { { 1, 2, 3 }, 
                 { 4, 5, 6 }, 
                 { 7, 8, 9 } }; 
      
    diagonal(A); 
  
// This code is contributed by AnkitRai01

chevron_right


Output:

1 
2 4 
3 5 7 
6 8 
9

Time Complexity: Time complexity of the above solution is O(N*N).

Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01