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
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to print the matrix void printMatrix(vector<vector< int > >& mat, int N) { // Print the leftmost parenthesis // of the matrix cout << "{ " ; // Traverse the matrix for ( int i = 0; i < N; i++) { cout << "{ " ; // Traverse each column // of the matrix for ( int j = 0; j < N; j++) { // If current column is last // column of the matrix if (j == N - 1) { // Print current element // of the matrix cout << mat[i][j]; } // If current column is not // last column of the matrix else { cout << mat[i][j] << ", " ; } } cout << " } " ; } // Print the leftmost parenthesis // of the matrix cout << "}" ; } // Function to find the matrix with no two // adjacent elements equal by increment operation void MatNoAdjacentElemEq(vector<vector< int > >& mat, int N) { // Traverse the matrix for ( int i = 0; i < N; i++) { // Traverse column of the matrix for ( int j = 0; j < N; j++) { // If i is an even number if (i % 2 == 0) { // If j is an even number and current // matrix element is an odd number if (j % 2 == 0 && mat[i][j] % 2 != 0) { // Increment the value of // matrix element mat[i][j]++; } // If j is an odd number and current // matrix element is an even number else if (j % 2 != 0 && mat[i][j] % 2 == 0) { // Increment the value of // matrix element mat[i][j]++; } } // If i is an odd number else { // If j is an even number and current // matrix element is an even number if (j % 2 == 0 && mat[i][j] % 2 == 0) { // Increment the value of // matrix element mat[i][j]++; } // If j is an odd number and current // matrix element is an odd number else if (j % 2 != 0 && mat[i][j] % 2 != 0) { // Increment the value of // matrix element mat[i][j]++; } } } } // Print the matrix printMatrix(mat, N); } // Driver Code 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
// Java program to implement // the above approach import java.util.*; class GFG{ // Function to print the matrix static void printMatrix( int [][] mat, int N) { // Print the leftmost parenthesis // of the matrix System.out.print( "{ " ); // Traverse the matrix for ( int i = 0 ; i < N; i++) { System.out.print( "{ " ); // Traverse each column // of the matrix for ( int j = 0 ; j < N; j++) { // If current column is last // column of the matrix if (j == N - 1 ) { // Print current element // of the matrix System.out.print(mat[i][j]); } // If current column is not // last column of the matrix else { System.out.print(mat[i][j] + ", " ); } } System.out.print( " } " ); } // Print the leftmost parenthesis // of the matrix System.out.print( "}" ); } // Function to find the matrix with no two // adjacent elements equal by increment operation static void MatNoAdjacentElemEq( int [][] mat, int N) { // Traverse the matrix for ( int i = 0 ; i < N; i++) { // Traverse column of the matrix for ( int j = 0 ; j < N; j++) { // If i is an even number if (i % 2 == 0 ) { // If j is an even number and current // matrix element is an odd number if (j % 2 == 0 && mat[i][j] % 2 != 0 ) { // Increment the value of // matrix element mat[i][j]++; } // If j is an odd number and current // matrix element is an even number else if (j % 2 != 0 && mat[i][j] % 2 == 0 ) { // Increment the value of // matrix element mat[i][j]++; } } // If i is an odd number else { // If j is an even number and current // matrix element is an even number if (j % 2 == 0 && mat[i][j] % 2 == 0 ) { // Increment the value of // matrix element mat[i][j]++; } // If j is an odd number and current // matrix element is an odd number else if (j % 2 != 0 && mat[i][j] % 2 != 0 ) { // Increment the value of // matrix element mat[i][j]++; } } } } // Print the matrix printMatrix(mat, N); } // Driver Code public static void main(String[] args) { int [][] mat = { { 1 , 2 , 2 }, { 3 , 2 , 2 }, { 2 , 2 , 2 } }; int N = mat.length; MatNoAdjacentElemEq(mat, N); } } // This code is contributed by aashish1995 |
Python3
# Python3 program to implement # the above approach # Function to print the matrix def printMatrix(mat, N): # Print the leftmost parenthesis # of the matrix print ( "{ " ,end = "") # Traverse the matrix for i in range (N): print ( "{ " ,end = "") # Traverse each column # of the matrix for j in range (N): # If current column is last # column of the matrix if (j = = N - 1 ): # Print current element # of the matrix print (mat[i][j], end = "") # If current column is not # last column of the matrix else : print (mat[i][j], end = ", " ) print ( " } " ,end = "") # Print the leftmost parenthesis # of the matrix print ( "}" ,end = "") # Function to find the matrix with no two # adjacent elements equal by increment operation def MatNoAdjacentElemEq(mat,N): # Traverse the matrix for i in range (N): # Traverse column of the matrix for j in range (N): # If i is an even number if (i % 2 = = 0 ): # If j is an even number and current # matrix element is an odd number if (j % 2 = = 0 and mat[i][j] % 2 ! = 0 ): # Increment the value of # matrix element mat[i][j] + = 1 # If j is an odd number and current # matrix element is an even number elif (j % 2 ! = 0 and mat[i][j] % 2 = = 0 ): # Increment the value of # matrix element mat[i][j] + = 1 # If i is an odd number else : # If j is an even number and current # matrix element is an even number if (j % 2 = = 0 and mat[i][j] % 2 = = 0 ): #Increment the value of #matrix element mat[i][j] + = 1 # If j is an odd number and current # matrix element is an odd number elif (j % 2 ! = 0 and mat[i][j] % 2 ! = 0 ): # Increment the value of # matrix element mat[i][j] + = 1 # Print the matrix printMatrix(mat, N) # Driver Code if __name__ = = '__main__' : mat = [[ 1 , 2 , 2 ], [ 3 , 2 , 2 ], [ 2 , 2 , 2 ]] N = len (mat) MatNoAdjacentElemEq(mat, N) # This code is contributed by mohit kumar 29 |
C#
// C# program to implement // the above approach using System; class GFG{ // Function to print the matrix static void printMatrix( int [,] mat, int N) { // Print the leftmost parenthesis // of the matrix Console.Write( "{ " ); // Traverse the matrix for ( int i = 0; i < N; i++) { Console.Write( "{ " ); // Traverse each column // of the matrix for ( int j = 0; j < N; j++) { // If current column is last // column of the matrix if (j == N - 1) { // Print current element // of the matrix Console.Write(mat[i, j]); } // If current column is not // last column of the matrix else { Console.Write(mat[i, j] + ", " ); } } Console.Write( " } " ); } // Print the leftmost parenthesis // of the matrix Console.Write( "}" ); } // Function to find the matrix with no two // adjacent elements equal by increment operation static void MatNoAdjacentElemEq( int [,] mat, int N) { // Traverse the matrix for ( int i = 0; i < N; i++) { // Traverse column of the matrix for ( int j = 0; j < N; j++) { // If i is an even number if (i % 2 == 0) { // If j is an even number and current // matrix element is an odd number if (j % 2 == 0 && mat[i, j] % 2 != 0) { // Increment the value of // matrix element mat[i, j]++; } // If j is an odd number and current // matrix element is an even number else if (j % 2 != 0 && mat[i, j] % 2 == 0) { // Increment the value of // matrix element mat[i, j]++; } } // If i is an odd number else { // If j is an even number and current // matrix element is an even number if (j % 2 == 0 && mat[i, j] % 2 == 0) { // Increment the value of // matrix element mat[i, j]++; } // If j is an odd number and current // matrix element is an odd number else if (j % 2 != 0 && mat[i, j] % 2 != 0) { // Increment the value of // matrix element mat[i, j]++; } } } } // Print the matrix printMatrix(mat, N); } // Driver Code 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); } } // This code is contributed by aashish1995 |
Javascript
<script> // Javascript program to implement // the above approach // Function to print the matrix function printMatrix(mat,N) { // Print the leftmost parenthesis // of the matrix document.write( "{ " ); // Traverse the matrix for (let i = 0; i < N; i++) { document.write( "{ " ); // Traverse each column // of the matrix for (let j = 0; j < N; j++) { // If current column is last // column of the matrix if (j == N - 1) { // Print current element // of the matrix document.write(mat[i][j]); } // If current column is not // last column of the matrix else { document.write(mat[i][j] + ", " ); } } document.write( " } " ); } // Print the leftmost parenthesis // of the matrix document.write( "}" ); } // Function to find the matrix with no two // adjacent elements equal by increment operation function MatNoAdjacentElemEq(mat, N) { // Traverse the matrix for (let i = 0; i < N; i++) { // Traverse column of the matrix for (let j = 0; j < N; j++) { // If i is an even number if (i % 2 == 0) { // If j is an even number and current // matrix element is an odd number if (j % 2 == 0 && mat[i][j] % 2 != 0) { // Increment the value of // matrix element mat[i][j]++; } // If j is an odd number and current // matrix element is an even number else if (j % 2 != 0 && mat[i][j] % 2 == 0) { // Increment the value of // matrix element mat[i][j]++; } } // If i is an odd number else { // If j is an even number and current // matrix element is an even number if (j % 2 == 0 && mat[i][j] % 2 == 0) { // Increment the value of // matrix element mat[i][j]++; } // If j is an odd number and current // matrix element is an odd number else if (j % 2 != 0 && mat[i][j] % 2 != 0) { // Increment the value of // matrix element mat[i][j]++; } } } } // Print the matrix printMatrix(mat, N); } // Driver Code let mat = [[ 1, 2, 2 ], [ 3, 2, 2 ], [ 2, 2, 2 ]]; let N = mat.length; MatNoAdjacentElemEq(mat, N); // This code is contributed by unknown2108 </script> |
{ { 2, 3, 2 } { 3, 2, 3 } { 2, 3, 2 } }
Time Complexity: O(N2)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!