Construct a square Matrix whose parity of diagonal sum is same as size of matrix
Last Updated :
31 Jan, 2022
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.
Examples:
Input: N = 4
Output:
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13
Explanation:
Sum of diagonal = 32 and 36 and integer N = 4, all the numbers are even that is same parity.
Input: N = 3
Output:
1 2 3
6 5 4
7 8 9
Explanation:
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:
C++
#include<bits/stdc++.h>
using namespace std;
void createMatrix( int N)
{
int M[N][N];
int count = 1;
for ( int i = 0; i < N; i++) {
if (i % 2 == 0) {
for ( int j = 0; j < N; j++) {
M[i][j] = count;
count++;
}
}
else
{
for ( int j = N - 1;j >= 0; j--){
M[i][j] = count;
count += 1;
}
}
}
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < N; j++) {
cout << M[i][j] << " " ;
}
cout << endl;
}
}
int main()
{
int N = 3;
createMatrix(N);
return 0;
}
|
Java
class GFG{
static void createMatrix( int N)
{
int M[][] = new int [N][N];
int count = 1 ;
for ( int i = 0 ; i < N; i++)
{
if (i % 2 == 0 )
{
for ( int j = 0 ; j < N; j++)
{
M[i][j] = count;
count++;
}
}
else
{
for ( int j = N - 1 ; j >= 0 ; j--){
M[i][j] = count;
count += 1 ;
}
}
}
for ( int i = 0 ; i < N; i++)
{
for ( int j = 0 ; j < N; j++)
{
System.out.print(M[i][j] + " " );
}
System.out.println();
}
}
public static void main(String[] args)
{
int N = 3 ;
createMatrix(N);
}
}
|
Python3
def createMatrix(N):
M = [[ 0 ] * N for i in range (N)]
count = 1
for i in range (N):
if (i % 2 = = 0 ):
for j in range (N):
M[i][j] = count
count + = 1
else :
for j in range (N - 1 , - 1 , - 1 ):
M[i][j] = count
count + = 1
for i in range (N):
for j in range (N):
print (M[i][j], end = " " )
print ()
if __name__ = = '__main__' :
N = 3
createMatrix(N)
|
C#
using System;
class GFG{
static void createMatrix( int N)
{
int [,] M = new int [N, N];
int count = 1;
for ( int i = 0; i < N; i++)
{
if (i % 2 == 0)
{
for ( int j = 0; j < N; j++)
{
M[i, j] = count;
count++;
}
}
else
{
for ( int j = N - 1; j >= 0; j--)
{
M[i, j] = count;
count += 1;
}
}
}
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < N; j++)
{
Console.Write(M[i, j] + " " );
}
Console.WriteLine();
}
}
public static void Main()
{
int N = 3;
createMatrix(N);
}
}
|
Javascript
<script>
function createMatrix(N)
{
var M = Array(N).fill(0).map(x => Array(N).fill(0));
var count = 1;
for (i = 0; i < N; i++)
{
if (i % 2 == 0)
{
for (j = 0; j < N; j++)
{
M[i][j] = count;
count++;
}
}
else
{
for (j = N - 1; j >= 0; j--){
M[i][j] = count;
count += 1 ;
}
}
}
for (i = 0; i < N; i++)
{
for (j = 0; j < N; j++)
{
document.write(M[i][j] + " " );
}
document.write( "<br>" );
}
}
var N = 3;
createMatrix(N);
</script>
|
Output:
1 2 3
6 5 4
7 8 9
Time Complexity: O(N*N)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...