Modify matrix by increments such that no pair of adjacent elements are equal
Last Updated :
31 Mar, 2022
Given a square matrix mat[][] of size N * N, the task is to print the matrix after incrementing matrix elements such that no two adjacent elements of the matrix are equal.
Examples:
Input: mat[][] = { { 1, 2, 2 }, { 3, 2, 2 }, { 2, 2, 2 } }
Output: { { 2, 3, 2 }, { 3, 2, 3 }, { 2, 3, 2 } }
Explanation:
Incrementing the value of { mat[0][0], mat[0][1], mat[1][2], mat[2][1] } by 1 modifies the matrix to { { 2, 3, 2 }, { 3, 2, 3 }, { 2, 3, 2 } }, where no two adjacent elements are equal.
Therefore, the required output is { { 2, 3, 2 }, { 3, 2, 3 }, { 2, 3, 2 } }.
Input: mat[][] = { { 1, 2 }, { 1, 2 } }
Output: { { 2, 3 }, {1, 2 } }
Approach: Follow the steps below to solve the problem:
- Traverse the matrix using variable (i, j) and check the following conditions.
- If i % 2 == 0 and j % 2 == 0 and mat[i][j] % 2 == 1, then increment the value of mat[i][j] by 1.
- If i % 2 == 0 and j % 2 == 1 and mat[i][j] % 2 == 0, then increment the value of mat[i][j] by 1.
- If i % 2 == 1 and j % 2 == 0 and mat[i][j] % 2 == 0, then increment the value of mat[i][j] by 1.
- If i % 2 == 1 and j % 2 == 1 and mat[i][j] % 2 == 1, then increment the value of mat[i][j] by 1.
- Finally, print the matrix mat[][].
Below is the implementation of the above approach:
C++14
#include <bits/stdc++.h>
using namespace std;
void printMatrix(vector<vector< int > >& mat, int N)
{
cout << "{ " ;
for ( int i = 0; i < N; i++) {
cout << "{ " ;
for ( int j = 0; j < N; j++) {
if (j == N - 1) {
cout << mat[i][j];
}
else {
cout << mat[i][j] << ", " ;
}
}
cout << " } " ;
}
cout << "}" ;
}
void MatNoAdjacentElemEq(vector<vector< int > >& mat,
int N)
{
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < N; j++) {
if (i % 2 == 0) {
if (j % 2 == 0 && mat[i][j] % 2 != 0) {
mat[i][j]++;
}
else if (j % 2 != 0 && mat[i][j] % 2 == 0) {
mat[i][j]++;
}
}
else {
if (j % 2 == 0 && mat[i][j] % 2 == 0) {
mat[i][j]++;
}
else if (j % 2 != 0 && mat[i][j] % 2 != 0) {
mat[i][j]++;
}
}
}
}
printMatrix(mat, N);
}
int main()
{
vector<vector< int > > mat = { { 1, 2, 2 },
{ 3, 2, 2 },
{ 2, 2, 2 } };
int N = mat.size();
MatNoAdjacentElemEq(mat, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void printMatrix( int [][] mat, int N)
{
System.out.print( "{ " );
for ( int i = 0 ; i < N; i++)
{
System.out.print( "{ " );
for ( int j = 0 ; j < N; j++)
{
if (j == N - 1 )
{
System.out.print(mat[i][j]);
}
else
{
System.out.print(mat[i][j] + ", " );
}
}
System.out.print( " } " );
}
System.out.print( "}" );
}
static void MatNoAdjacentElemEq( int [][] mat,
int N)
{
for ( int i = 0 ; i < N; i++)
{
for ( int j = 0 ; j < N; j++)
{
if (i % 2 == 0 )
{
if (j % 2 == 0 &&
mat[i][j] % 2 != 0 )
{
mat[i][j]++;
}
else if (j % 2 != 0 &&
mat[i][j] % 2 == 0 )
{
mat[i][j]++;
}
}
else
{
if (j % 2 == 0 &&
mat[i][j] % 2 == 0 )
{
mat[i][j]++;
}
else if (j % 2 != 0 &&
mat[i][j] % 2 != 0 )
{
mat[i][j]++;
}
}
}
}
printMatrix(mat, N);
}
public static void main(String[] args)
{
int [][] mat = { { 1 , 2 , 2 },
{ 3 , 2 , 2 },
{ 2 , 2 , 2 } };
int N = mat.length;
MatNoAdjacentElemEq(mat, N);
}
}
|
Python3
def printMatrix(mat, N):
print ( "{ " ,end = "")
for i in range (N):
print ( "{ " ,end = "")
for j in range (N):
if (j = = N - 1 ):
print (mat[i][j], end = "")
else :
print (mat[i][j], end = ", " )
print ( " } " ,end = "")
print ( "}" ,end = "")
def MatNoAdjacentElemEq(mat,N):
for i in range (N):
for j in range (N):
if (i % 2 = = 0 ):
if (j % 2 = = 0 and mat[i][j] % 2 ! = 0 ):
mat[i][j] + = 1
elif (j % 2 ! = 0 and mat[i][j] % 2 = = 0 ):
mat[i][j] + = 1
else :
if (j % 2 = = 0 and mat[i][j] % 2 = = 0 ):
mat[i][j] + = 1
elif (j % 2 ! = 0 and mat[i][j] % 2 ! = 0 ):
mat[i][j] + = 1
printMatrix(mat, N)
if __name__ = = '__main__' :
mat = [[ 1 , 2 , 2 ],
[ 3 , 2 , 2 ],
[ 2 , 2 , 2 ]]
N = len (mat)
MatNoAdjacentElemEq(mat, N)
|
C#
using System;
class GFG{
static void printMatrix( int [,] mat, int N)
{
Console.Write( "{ " );
for ( int i = 0; i < N; i++)
{
Console.Write( "{ " );
for ( int j = 0; j < N; j++)
{
if (j == N - 1)
{
Console.Write(mat[i, j]);
}
else
{
Console.Write(mat[i, j] + ", " );
}
}
Console.Write( " } " );
}
Console.Write( "}" );
}
static void MatNoAdjacentElemEq( int [,] mat,
int N)
{
for ( int i = 0; i < N; i++)
{
for ( int j = 0; j < N; j++)
{
if (i % 2 == 0)
{
if (j % 2 == 0 &&
mat[i, j] % 2 != 0)
{
mat[i, j]++;
}
else if (j % 2 != 0 &&
mat[i, j] % 2 == 0)
{
mat[i, j]++;
}
}
else
{
if (j % 2 == 0 &&
mat[i, j] % 2 == 0)
{
mat[i, j]++;
}
else if (j % 2 != 0 &&
mat[i, j] % 2 != 0)
{
mat[i, j]++;
}
}
}
}
printMatrix(mat, N);
}
public static void Main(String[] args)
{
int [,] mat = { { 1, 2, 2 },
{ 3, 2, 2 },
{ 2, 2, 2 } };
int N = mat.GetLength(0);
MatNoAdjacentElemEq(mat, N);
}
}
|
Javascript
<script>
function printMatrix(mat,N)
{
document.write( "{ " );
for (let i = 0; i < N; i++)
{
document.write( "{ " );
for (let j = 0; j < N; j++)
{
if (j == N - 1)
{
document.write(mat[i][j]);
}
else
{
document.write(mat[i][j] + ", " );
}
}
document.write( " } " );
}
document.write( "}" );
}
function MatNoAdjacentElemEq(mat, N)
{
for (let i = 0; i < N; i++)
{
for (let j = 0; j < N; j++)
{
if (i % 2 == 0)
{
if (j % 2 == 0 &&
mat[i][j] % 2 != 0)
{
mat[i][j]++;
}
else if (j % 2 != 0 &&
mat[i][j] % 2 == 0)
{
mat[i][j]++;
}
}
else
{
if (j % 2 == 0 &&
mat[i][j] % 2 == 0)
{
mat[i][j]++;
}
else if (j % 2 != 0 &&
mat[i][j] % 2 != 0)
{
mat[i][j]++;
}
}
}
}
printMatrix(mat, N);
}
let mat = [[ 1, 2, 2 ],
[ 3, 2, 2 ],
[ 2, 2, 2 ]];
let N = mat.length;
MatNoAdjacentElemEq(mat, N);
</script>
|
Output:
{ { 2, 3, 2 } { 3, 2, 3 } { 2, 3, 2 } }
Time Complexity: O(N2)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...