Given an integer N. The task is to generate a square matrix of ( n x n ) having the elements ranging from 1 to n^2 with the following condition:
- The elements of the matrix should be distinct i.e used only once
- Numbers ranging from 1 to n^2
- Every sub-matrix you choose should have the sum of opposite corner elements as even i.e sum of top left and bottom right should be even and the sum of top right and bottom left element should be even
This property should apply to all the submatrices of the matrix. You need to generate an Even Sub-Matrix
Examples:
Input: 2
Output: 1 2
4 3
Explanation: Here sum of 1+3=4 is even and 2+4=6 is even
Input: 4
Output: 1 2 3
4 5 6
7 8 9
Explanation: The sub matrix [1 2 4 5], [2 3 5 6], [4 5 7 8], [5 6 8 9], [1 2 3 4 5 6 7 8 9] satisfies the condition of opposite corner
elements having even sum
Approach:
As we know for any two elements sum to be even it can be Sum of ODD and ODD or Sum of EVEN and EVEN. In either of the two cases for the corner elements sum to be even we need to ensure that the diagonal pattern arranged elements should be either odd or even. So we make the 2d array having diagonals as all odds or all evens to find any submatrix having corner elements sum even. The below approach can be followed for the same.
- When n is odd the diagonals are already in all odd or even elements, so we need not modify and generate a simple 2d array
- When n is even the matrix generated does not satisfy the property having an even sum of opposite corner elements of the sub-matrices, so we reverse the alternate row elements so that diagonals of every submatrix are either all odd or all even.
Below is the implementation.
C++
#include <bits/stdc++.h>
using namespace std;
void sub_mat_even( int N)
{
int K = 1;
int A[N][N];
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < N; j++)
{
A[i][j] = K;
K++;
}
}
if (N % 2 == 0)
{
for ( int i = 0; i < N; i++)
{
if (i % 2 == 1)
{
int s = 0;
int l = N - 1;
while (s < l)
{
swap(A[i][s],
A[i][l]);
s++;
l--;
}
}
}
}
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < N; j++)
{
cout << A[i][j] << " " ;
}
cout << endl;
}
}
int main()
{
int N = 4;
sub_mat_even(N);
}
|
Java
import java.io.*;
class GFG{
static void sub_mat_even( int N)
{
int K = 1 ;
int [][] A = new int [N][N];
for ( int i = 0 ; i < N; i++)
{
for ( int j = 0 ; j < N; j++)
{
A[i][j] = K;
K++;
}
}
if (N % 2 == 0 )
{
for ( int i = 0 ; i < N; i++)
{
if (i % 2 == 1 )
{
int s = 0 ;
int l = N - 1 ;
while (s < l)
{
swap(A[i], s, l);
s++;
l--;
}
}
}
}
for ( int i = 0 ; i < N; i++)
{
for ( int j = 0 ; j < N; j++)
{
System.out.print(A[i][j] + " " );
}
System.out.println();
}
}
private static void swap( int [] A, int s, int l)
{
int temp = A[s];
A[s] = A[l];
A[l] = temp;
}
public static void main(String[] args)
{
int N = 4 ;
sub_mat_even(N);
}
}
|
Python3
import itertools
def sub_mat_even(n):
temp = itertools.count( 1 )
l = [[ next (temp) for i in range (n)] for i in range (n)]
if n % 2 = = 0 :
for i in range ( 0 , len (l)):
if i % 2 = = 1 :
l[i][:] = l[i][:: - 1 ]
for i in range (n):
for j in range (n):
print (l[i][j],end = " " )
print ()
n = 4
sub_mat_even(n)
|
C#
using System;
class GFG {
static void sub_mat_even( int N)
{
int K = 1;
int [,] A = new int [N, N];
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < N; j++)
{
A[i, j] = K;
K++;
}
}
if (N % 2 == 0)
{
for ( int i = 0; i < N; i++)
{
if (i % 2 == 1)
{
int s = 0;
int l = N - 1;
while (s < l)
{
int temp = A[i, s];
A[i, s] = A[i, l];
A[i, l] = temp;
s++;
l--;
}
}
}
}
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < N; j++)
{
Console.Write(A[i, j] + " " );
}
Console.WriteLine();
}
}
static void Main() {
int N = 4;
sub_mat_even(N);
}
}
|
Output:
1 2 3 4
8 7 6 5
9 10 11 12
16 15 14 13
This approach takes O(n*2) time complexity.