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:
Input : Output : 1 2 5 3 6 9 4 7 10 13 8 11 14 12 15 16
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:
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.
- Return previous element in an expanding matrix
- Check if matrix can be converted to another matrix by transposing square sub-matrices
- Maximum points from top left of matrix to bottom right and return back
- Count of ways to traverse a Matrix and return to origin in K steps
- Minimum time to return array to its original state after given modifications
- Check whether a Matrix is a Latin Square or not
- Maximum and Minimum in a square matrix.
- Maximum size square sub-matrix with all 1s
- How to access elements of a Square Matrix
- Sum of non-diagonal parts of a square Matrix
- Check given matrix is magic square or not
- Product of middle row and column in an odd square matrix
- Print maximum sum square sub-matrix of given size
- Sum of both diagonals of a spiral odd-order square matrix
- Find the product of sum of two diagonals of a square Matrix
- Print all the sub diagonal elements of the given square matrix
- Count all square sub-matrix with sum greater than the given number S
- Given an n x n square matrix, find sum of all sub-squares of size k x k
- Sum of all parts of a square Matrix divided by its diagonals
- Inplace rotate square matrix by 90 degrees | Set 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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