Make a N*N matrix that contains integers from 1 to N^2 having maximum adjacent difference
Last Updated :
30 Jan, 2023
Given an integer N. Return a N x N matrix such that each element (Coordinates(i, j))is having maximum absolute difference possible with the adjacent element (i.e., (i + 1, j), (i – 1, j), (i, j + 1), (i, j – 1)). Also, It should contain distinct elements ranging from 1 to N2.
Examples:
Input: N = 2
Output: 4 1
2 3
Input: N = 3
Output: 1 3 4
9 2 7
5 8 6
Approach: The problem can be solved based on the following idea:
The distinct numbers don’t exceed N2 – 1, because the minimum difference between two elements is at least 1, and the maximum difference does not exceed N2 – 1 (the difference between the maximum element N2 and the minimum element 1).
Follow the below steps to implement the idea:
- Initialize an empty 2D matrix v of size N*N and bool check = true.
- Start adding the elements from 1 to N2 i.e., l and r pointers respectively.
- If check = true, start adding the elements from the r pointer in decreasing order until the row is filled and change the check = false.
- If check = false, start adding the elements from the l pointer in increasing order until the row is filled and change the check = true.
- Repeat this until all matrix is filled.
Below is the implementation of the above approach:
Java
import java.util.Arrays;
import java.util.Scanner;
public class Main {
static void findMaximumDistinct( int n, int [][] v)
{
int l = 1 , r = n * n;
boolean check = true ;
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < n; j++) {
if (check == false ) {
v[i][j] = l++;
check = true ;
}
else {
v[i][j] = r--;
check = false ;
}
}
if (i % 2 == 1 ) {
for ( int j = 0 ; j < n / 2 ; j++) {
int temp = v[i][j];
v[i][j] = v[i][n - j - 1 ];
v[i][n - j - 1 ] = temp;
}
}
}
}
public static void main(String[] args)
{
int n = 3 ;
int [][] v = new int [n][n];
findMaximumDistinct(n, v);
for ( int i = 0 ; i < n; i++) {
for ( int j = 0 ; j < n; j++) {
System.out.print(v[i][j] + " " );
}
System.out.println();
}
}
}
|
Python3
def findMaximumDistinct(n, v):
l = 1
r = n * n
check = True
for i in range (n):
for j in range (n):
if check = = False :
v[i][j] = l
l + = 1
check = True
else :
v[i][j] = r
r - = 1
check = False
if i % 2 = = 1 :
for j in range (n / / 2 ):
temp = v[i][j]
v[i][j] = v[i][n - j - 1 ]
v[i][n - j - 1 ] = temp
n = 3
v = [[ 0 for i in range (n)] for j in range (n)]
findMaximumDistinct(n, v)
for i in range (n):
for j in range (n):
print (v[i][j], end = " " )
print ()
|
C#
using System;
using System.Linq;
class Program {
public static void findMaximumDistinct( int n, int [][] v)
{
int l = 1, r = n * n;
bool check = true ;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
if (check == false ) {
v[i][j] = l;
l++;
check = true ;
}
else {
v[i][j] = r;
r--;
check = false ;
}
}
if (i % 2 != 0) {
v[i] = v[i].Reverse().ToArray();
}
}
}
public static void Main()
{
int n = 3;
int [][] v = new int [n][];
for ( int i = 0; i < n; i++) {
v[i] = new int [n];
}
findMaximumDistinct(n, v);
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
Console.Write(v[i][j] + " " );
}
Console.WriteLine();
}
}
}
|
Javascript
function findMaximumDistinct(n, v)
{
let l = 1, r = n * n;
let check = true ;
for (let i = 0; i < n; i++)
{
for (let j = 0; j < n; j++)
{
if (check === false )
{
v[i][j] = l++;
check = true ;
}
else
{
v[i][j] = r--;
check = false ;
}
}
if (i % 2 === 1) {
for (let j = 0; j < n / 2; j++) {
let temp = v[i][j];
v[i][j] = v[i][n - j - 1];
v[i][n - j - 1] = temp;
}
}
}
}
let n = 3;
let v = new Array(n).fill(0).map(() => new Array(n));
findMaximumDistinct(n, v);
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
console.log(v[i][j] + " " );
}
console.log( "<br>" );
}
|
C++14
#include<bits/stdc++.h>
using namespace std;
void findMaximumDistinct( int n, vector<vector< int >>& v)
{
int l = 1, r = n * n;
bool check = true ;
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
if (check == false ) {
v[i][j] = l++;
check = true ;
}
else {
v[i][j] = r--;
check = false ;
}
}
if (i % 2 == 1) {
for ( int j = 0; j < n / 2; j++) {
int temp = v[i][j];
v[i][j] = v[i][n - j - 1];
v[i][n - j - 1] = temp;
}
}
}
}
int main()
{
int n = 3;
vector<vector< int >> v(n, vector< int >(n));
findMaximumDistinct(n, v);
for ( int i = 0; i < n; i++) {
for ( int j = 0; j < n; j++) {
cout<<v[i][j] << " " ;
}
cout<<endl;
}
}
|
Time Complexity: O(n2)
Auxiliary Space: O(n2)
Share your thoughts in the comments
Please Login to comment...