# 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 : Output :
1
2  5
3  6  9
4  7  10  13
8  11 14
12 15
16
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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++

 `// CPP implementation to  return ` `// an array of its anti-diagonals ` `// when an N*N square matrix is given ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// 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) { ` `            ``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 = { { 1, 2, 3 }, ` `                    ``{ 4, 5, 6 }, ` `                    ``{ 7, 8, 9 } }; ` ` `  `    ``diagonal(A); ` ` `  `    ``return` `0; ` `} `

## Java

 `// 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); ` `    ``} ` `} `

## Python3

 `# 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 `

## C#

 `// 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 `

Output:

```1
2 4
3 5 7
6 8
9
```

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

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

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