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
- Construct a Matrix whose sum of diagonals for each square submatrix is even
- Filling diagonal to make the sum of every row, column and diagonal equal of 3x3 matrix
- Program to swap upper diagonal elements with lower diagonal elements of matrix.
- Sum of non-diagonal parts of a square Matrix
- Count numbers which can be represented as sum of same parity primes
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Length of a Diagonal of a Parallelogram using the length of Sides and the other Diagonal
- Find a subarray of size K whose sum is a perfect square
- Print all the sub diagonal elements of the given square matrix
- Print all the super diagonal elements of the given square matrix
- Maximum size of square such that all submatrices of that size have sum less than K
- 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
- Check if two elements of a matrix are on the same diagonal or not
- Construct an Array of size N in which sum of odd elements is equal to sum of even elements
- Minimum flips in a Binary array such that XOR of consecutive subarrays of size K have different parity
- Construct an Array such that cube sum of all element is a perfect square
- Construct a Matrix of size NxN with values in range [1, N^2] as per given conditions
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.