Given an integer N representing the size of the matrix, the task is to construct a square matrix N * N which have an element from 1 to N2 such that the parity of the sum of its diagonals is equal to the parity of integer N.
Input: N = 4
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13
Sum of diagonal = 32 and 36 and integer N = 4, all the numbers are even that is same parity.
Input: N = 3
1 2 3
6 5 4
7 8 9
Sum of diagonal = 15 and integer N = 3, all the numbers are odd that is same parity.
Approach: The idea is to observe that on filling the elements in the matrix in an alternative fashion the parity of N and the sum of diagonals is the same. Start the counter from 1 and then fill the first row from 0 to N – 1 in increasing order, then fill the second row from index N – 1 to 0, and so on. Keep filling each element from value 1 to N2 in this alternate fashion to get the required matrix.
Below is the implementation of the above approach:
1 2 3 6 5 4 7 8 9
Time Complexity: O(N*N)
Auxiliary Space: O(1)
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.
- Construct an Array of size N whose sum of cube of all elements is a perfect square
- Construct a matrix with sum equal to the sum of diagonal elements
- 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
- Program to swap upper diagonal elements with lower diagonal elements of matrix.
- Count numbers which can be represented as sum of same parity primes
- Sum of non-diagonal parts of a square Matrix
- Generate a matrix having sum of secondary diagonal equal to a perfect square
- Length of a Diagonal of a Parallelogram using the length of Sides and the other Diagonal
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Check if all the elements can be made of same parity by inverting adjacent elements
- Count of all possible pairs of array elements with same parity
- Print all the sub diagonal elements of the given square matrix
- Print all the super diagonal elements of the given square matrix
- Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity
- Find a subarray of size K whose sum is a perfect square
- Check if two elements of a matrix are on the same diagonal or not
- Maximum size of square such that all submatrices of that size have sum less than K
- Construct an Array of size N in which sum of odd elements is equal to sum of even elements
- Construct an Array such that cube sum of all element is a perfect square
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.