Construct a unique matrix n x n for an input n
Given an odd integer n, find a matrix of size n x n with the following conditions:
- Each cell contains an integer from 1 and n (inclusive).
- No integer appears twice in the same row or the same column.
- All 1’s must be at every possible distance from the center of the matrix. The center of a n x n square is cell ((n-1)/2, (n-1)/2) for odd n.
Example :
Input : n = 1
Output : 1
Input : n = 3
Output: 3 2 1
1 3 2
2 1 3
Input : n = 5
Output : 5 3 2 4 1
1 4 3 5 2
2 5 4 1 3
3 1 5 2 4
4 2 1 3 5
The idea is to first decide on positions of 1s. One possible arrangement of 1s for n = 5 is,
_ _ _ _ 1
1 _ _ _ _
_ _ _ 1 _
_ 1 _ _ _
_ _ 1 _ _
Once we have decided 1s, the task of filling the remaining items is simple. We fill the remaining entries column by column. For every 1, we traverse its column and fill all entries below it with 2, 3,…p and fill all entries above it with p+1, .. n. We get following.
5 3 2 4 1
1 4 3 5 2
2 5 4 1 3
3 1 5 2 4
4 2 1 3 5
To decide the initial positions of 1s, we traverse all rows and keep track of two column numbers “left” and “right”.
- “right” starts with n-1 and keeps decrementing after every alternate row.
- “left” starts with 0 and keeps incrementing after every alternate row.
Below are implementations of the above idea.
C++
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int MAX = 100;
int mat[MAX][MAX];
void fillRemaining( int i, int j, int n)
{
int x = 2;
for ( int k = i + 1; k < n; k++)
mat[k][j] = x++;
for ( int k = 0; k < i; k++)
mat[k][j] = x++;
}
void constructMatrix( int n)
{
int right = n - 1, left = 0;
for ( int i = 0; i < n; i++)
{
if (i % 2 == 0)
{
mat[i][right] = 1;
fillRemaining(i, right, n);
right--;
}
else
{
mat[i][left] = 1;
fillRemaining(i, left, n);
left++;
}
}
}
int main()
{
int n = 5;
constructMatrix(n);
for ( int i = 0; i < n; i++)
{
for ( int j = 0; j < n; j++)
printf ( "%d " ,mat[i][j]);
printf ( "\n" );
}
return 0;
}
|
Java
class GFG
{
static final int MAX = 100 ;
static int [][] mat = new int [MAX][MAX];
static void fillRemaining( int i, int j, int n)
{
int x = 2 ;
for ( int k = i + 1 ; k < n; k++)
mat[k][j] = x++;
for ( int k = 0 ; k < i; k++)
mat[k][j] = x++;
}
static void constructMatrix( int n)
{
int right = n - 1 , left = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (i % 2 == 0 )
{
mat[i][right] = 1 ;
fillRemaining(i, right, n);
right--;
}
else
{
mat[i][left] = 1 ;
fillRemaining(i, left, n);
left++;
}
}
}
public static void main(String args[])
{
int n = 5 ;
constructMatrix(n);
for ( int i = 0 ; i < n; i++)
{
for ( int j = 0 ; j < n; j++)
System.out.print(mat[i][j]+ " " );
System.out.println();
}
}
}
|
Python3
MAX = 100 ;
mat = [[ 0 for x in range ( MAX )] for y in range ( MAX )];
def fillRemaining(i, j, n):
x = 2 ;
for k in range (i + 1 ,n):
mat[k][j] = x;
x + = 1 ;
for k in range (i):
mat[k][j] = x;
x + = 1 ;
def constructMatrix(n):
right = n - 1 ;
left = 0 ;
for i in range (n):
if (i % 2 = = 0 ):
mat[i][right] = 1 ;
fillRemaining(i, right, n);
right - = 1 ;
else :
mat[i][left] = 1 ;
fillRemaining(i, left, n);
left + = 1 ;
n = 5 ;
constructMatrix(n);
for i in range (n):
for j in range (n):
print (mat[i][j],end = " " );
print ("");
|
C#
using System;
class GFG
{
static int MAX = 100;
static int [,]mat = new int [MAX, MAX];
static void fillRemaining( int i, int j, int n)
{
int x = 2;
for ( int k = i + 1; k < n; k++)
mat[k, j] = x++;
for ( int k = 0; k < i; k++)
mat[k, j] = x++;
}
static void constructMatrix( int n)
{
int right = n - 1, left = 0;
for ( int i = 0; i < n; i++)
{
if (i % 2 == 0)
{
mat[i, right] = 1;
fillRemaining(i, right, n);
right--;
}
else
{
mat[i, left] = 1;
fillRemaining(i, left, n);
left++;
}
}
}
public static void Main()
{
int n = 5;
constructMatrix(n);
for ( int i = 0; i < n; i++)
{
for ( int j = 0 ; j < n; j++)
Console.Write(mat[i, j]+ " " );
Console.WriteLine();
}
}
}
|
Javascript
<script>
var MAX = 100;
var mat = Array(MAX).fill(0).
map(x => Array(MAX).fill(0));
function fillRemaining(i , j , n)
{
var x = 2;
for (k = i + 1; k < n; k++)
mat[k][j] = x++;
for (k = 0; k < i; k++)
mat[k][j] = x++;
}
function constructMatrix(n)
{
var right = n - 1, left = 0;
for (i = 0; i < n; i++)
{
if (i % 2 == 0)
{
mat[i][right] = 1;
fillRemaining(i, right, n);
right--;
}
else
{
mat[i][left] = 1;
fillRemaining(i, left, n);
left++;
}
}
}
var n = 5;
constructMatrix(n);
for (i = 0; i < n; i++)
{
for (j = 0 ; j < n; j++)
document.write(mat[i][j]+ " " );
document.write( '<br>' );
}
</script>
|
Output
5 3 2 4 1
1 4 3 5 2
2 5 4 1 3
3 1 5 2 4
4 2 1 3 5
Time Complexity: O(n2)
Auxiliary Space: O(MAX2)
Last Updated :
18 Nov, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...