Given an integer N, the task is to print the given pattern.
Input: 3 Output: 1 2 4 3 5 7 6 8 9 Input: 4 Output: 1 2 4 7 3 5 8 11 6 9 12 14 10 13 15 16
- Create a matrix of size N X N which will store the pattern before printing.
- Store the elements in the upper triangle of the pattern. As observed the row index increases by 1 and column index decreases by 1 as you move down the diagonal.
- Once the upper triangle is completed then store the elements of the lower triangle in similar way as the upper triangle i.e. row index increases by 1 and column index decreases by 1 as you move down the diagonal.
Below is the implementation of the above approach:
1 2 4 3 5 7 6 8 9
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print matrix in diagonal pattern
- Program to swap upper diagonal elements with lower diagonal elements of matrix.
- Filling diagonal to make the sum of every row, column and diagonal equal of 3x3 matrix
- Maximum sum of elements in a diagonal parallel to the main diagonal of a given Matrix
- Length of a Diagonal of a Parallelogram using the length of Sides and the other Diagonal
- Program to check diagonal matrix and scalar matrix
- Program to convert given Matrix to a Diagonal Matrix
- Construct a square Matrix whose parity of diagonal sum is same as size of matrix
- Print all the sub diagonal elements of the given square matrix
- Print all the super diagonal elements of the given square matrix
- Print matrix in snake pattern
- Print lower triangular matrix pattern from given array
- Print concentric rectangular pattern in a 2d matrix
- Print matrix in snake pattern from the last column
- Mirror of matrix across diagonal
- Squares of Matrix Diagonal Elements
- Reverse Diagonal elements of matrix
- Find a Symmetric matrix of order N that contain integers from 0 to N-1 and main diagonal should contain only 0's
- Sum of non-diagonal parts of a square Matrix
- Program to convert the diagonal elements of the matrix to 0
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.