Skip to content
Related Articles

Related Articles

Modify matrix by increments such that no pair of adjacent elements are equal
  • Last Updated : 09 Dec, 2020

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 coloum 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 coloum 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to implement
# the above approach
 
# Function to prthe  matrix
def printMatrix(mat, N):
   
    # Prthe 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):
 
                # Prcurrent 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 = "")
 
    # Prthe 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 coloum 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
 
 
    # Prthe 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 coloum 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

chevron_right


Output: 

{ { 2, 3, 2 } { 3, 2, 3 } { 2, 3, 2 } }

 

Time Complexity: O(N2) 
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :