Skip to content
Related Articles

Related Articles

Improve Article
Modify a matrix by converting each element to XOR of its digits
  • Last Updated : 09 Jun, 2021

Given a matrix arr[][] of dimensions M*N, the task is to convert every matrix element to Bitwise XOR of digits present in the element.

Examples:

Input: arr[][] = {{27, 173}, {5, 21}}
Output:  
5 5
5 3
Explanation: 
Bitwise XOR of digits of arr[0][0] (= 27) is 5 (2^7).
Bitwise XOR value of digits of arr[0][1] (= 173) is 5 (1 ^ 7 ^ 3).
Bitwise XOR value of digits of arr[1][0] (= 5) is 5.
Bitwise XOR value of digits of arr[1][1] (= 21) is 3(1 ^ 2).

Input: arr[][] = {{11, 12, 33}, {64, 57, 61}, {74, 88, 39}}
Output:  
0 3 0  
2 2 7  
3 0 10

Approach: To solve the problem, the approach idea is to traverse the given matrix and for each matrix element, print the Bitwise XOR of its digits.



Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
const int M = 3;
const int N = 3;
 
// Function to calculate Bitwise
// XOR of digits present in X
int findXOR(int X)
{
 
    // Stores the Bitwise XOR
    int ans = 0;
 
    // While X is true
    while (X) {
 
        // Update Bitwise
        // XOR of its digits
        ans ^= (X % 10);
        X /= 10;
    }
 
    // Return the result
    return ans;
}
 
// Function to print matrix after
// converting each matrix element
// to XOR of its digits
void printXORmatrix(int arr[M][N])
{
    // Traverse each row of arr[][]
    for (int i = 0; i < M; i++) {
 
        // Traverse each column of arr[][]
        for (int j = 0; j < N; j++) {
            cout << arr[i][j] << " ";
        }
        cout << "\n";
    }
}
 
// Function to convert the given
// matrix to required XOR matrix
void convertXOR(int arr[M][N])
{
    // Traverse each row of arr[][]
    for (int i = 0; i < M; i++) {
 
        // Traverse each column of arr[][]
        for (int j = 0; j < N; j++) {
 
            // Store the current
            // matrix element
            int X = arr[i][j];
 
            // Find the xor of
            // digits present in X
            int temp = findXOR(X);
 
            // Stores the XOR value
            arr[i][j] = temp;
        }
    }
 
    // Print resultant matrix
    printXORmatrix(arr);
}
 
// Driver Code
int main()
{
    int arr[][3] = { { 27, 173, 5 },
                     { 21, 6, 624 },
                     { 5, 321, 49 } };
 
    convertXOR(arr);
 
    return 0;
}

Java




// Java program for the above approach
import java.io.*;
 
class GFG{
 
static int M = 3;
static int N = 3;
 
// Function to calculate Bitwise
// XOR of digits present in X
static int findXOR(int X)
{
     
    // Stores the Bitwise XOR
    int ans = 0;
 
    // While X is true
    while (X != 0)
    {
         
        // Update Bitwise
        // XOR of its digits
        ans ^= (X % 10);
        X /= 10;
    }
 
    // Return the result
    return ans;
}
 
// Function to print matrix after
// converting each matrix element
// to XOR of its digits
static void printXORmatrix(int arr[][])
{
     
    // Traverse each row of arr[][]
    for(int i = 0; i < M; i++)
    {
         
        // Traverse each column of arr[][]
        for(int j = 0; j < N; j++)
        {
            System.out.print(arr[i][j] + " ");
        }
        System.out.println();
    }
}
 
// Function to convert the given
// matrix to required XOR matrix
static void convertXOR(int arr[][])
{
     
    // Traverse each row of arr[][]
    for(int i = 0; i < M; i++)
    {
         
        // Traverse each column of arr[][]
        for(int j = 0; j < N; j++)
        {
             
            // Store the current
            // matrix element
            int X = arr[i][j];
 
            // Find the xor of
            // digits present in X
            int temp = findXOR(X);
 
            // Stores the XOR value
            arr[i][j] = temp;
        }
    }
 
    // Print resultant matrix
    printXORmatrix(arr);
}
 
// Driver Code
public static void main (String[] args)
{
    int arr[][] = { { 27, 173, 5 },
                    { 21, 6, 624 },
                    { 5, 321, 49 } };
 
    convertXOR(arr);
}
}
 
// This code is contributed by sanjoy_62

Python3




# Python3 program for the above approach
M = 3
N = 3
 
# Function to calculate Bitwise
# XOR of digits present in X
def findXOR(X):
 
    # Stores the Bitwise XOR
    ans = 0
 
    # While X is true
    while (X):
 
        # Update Bitwise
        # XOR of its digits
        ans ^= (X % 10)
        X //= 10
 
    # Return the result
    return ans
 
# Function to print matrix after
# converting each matrix element
# to XOR of its digits
def printXORmatrix(arr):
   
    # Traverse each row of arr[][]
    for i in range(3):
 
        # Traverse each column of arr[][]
        for j in range(3):
            print(arr[i][j], end = " ")
        print()
 
# Function to convert the given
# matrix to required XOR matrix
def convertXOR(arr):
   
    # Traverse each row of arr[][]
    for i in range(3):
 
        # Traverse each column of arr[][]
        for j in range(3):
 
            # Store the current
            # matrix element
            X = arr[i][j]
 
            # Find the xor of
            # digits present in X
            temp = findXOR(X)
 
            # Stores the XOR value
            arr[i][j] = temp
 
    # Print resultant matrix
    printXORmatrix(arr)
 
# Driver Code
if __name__ == '__main__':
    arr=[[27, 173, 5],
        [ 21, 6, 624 ],
        [ 5, 321, 49 ]]
 
    convertXOR(arr)
 
# This code is contributed by mohit kumar 29.

C#




// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
static int M = 3;
static int N = 3;
 
// Function to calculate Bitwise
// XOR of digits present in X
static int findXOR(int X)
{
     
    // Stores the Bitwise XOR
    int ans = 0;
 
    // While X is true
    while (X != 0)
    {
         
        // Update Bitwise
        // XOR of its digits
        ans ^= (X % 10);
        X /= 10;
    }
 
    // Return the result
    return ans;
}
 
// Function to print matrix after
// converting each matrix element
// to XOR of its digits
static void printXORmatrix(int[,] arr)
{
     
    // Traverse each row of arr[][]
    for(int i = 0; i < M; i++)
    {
         
        // Traverse each column of arr[][]
        for(int j = 0; j < N; j++)
        {
            Console.Write(arr[i, j] + " ");
        }
        Console.WriteLine();
    }
}
 
// Function to convert the given
// matrix to required XOR matrix
static void convertXOR(int[,] arr)
{
     
    // Traverse each row of arr[][]
    for(int i = 0; i < M; i++)
    {
         
        // Traverse each column of arr[][]
        for(int j = 0; j < N; j++)
        {
             
            // Store the current
            // matrix element
            int X = arr[i, j];
 
            // Find the xor of
            // digits present in X
            int temp = findXOR(X);
 
            // Stores the XOR value
            arr[i, j] = temp;
        }
    }
 
    // Print resultant matrix
    printXORmatrix(arr);
}
 
// Driver Code
static public void Main()
{
    int[,] arr = { { 27, 173, 5 },
                   { 21, 6, 624 },
                   { 5, 321, 49 } };
 
    convertXOR(arr);
}
}
 
// This code is contributed by splevel62

Javascript




<script>
 
// JavaScript program to implement
// the above approach
 
let M = 3;
let N = 3;
  
// Function to calculate Bitwise
// XOR of digits present in X
function findXOR(X)
{
      
    // Stores the Bitwise XOR
    let ans = 0;
  
    // While X is true
    while (X != 0)
    {
          
        // Update Bitwise
        // XOR of its digits
        ans ^= (X % 10);
        X /= 10;
    }
  
    // Return the result
    return ans;
}
  
// Function to print matrix after
// converting each matrix element
// to XOR of its digits
function printXORmatrix(arr)
{
      
    // Traverse each row of arr[][]
    for(let i = 0; i < M; i++)
    {
          
        // Traverse each column of arr[][]
        for(let j = 0; j < N; j++)
        {
            document.write(arr[i][j] + " ");
        }
        document.write("<br/>");
    }
}
  
// Function to convert the given
// matrix to required XOR matrix
function convertXOR(arr)
{
      
    // Traverse each row of arr[][]
    for(let i = 0; i < M; i++)
    {
          
        // Traverse each column of arr[][]
        for(let j = 0; j < N; j++)
        {
              
            // Store the current
            // matrix element
            let X = arr[i][j];
  
            // Find the xor of
            // digits present in X
            let temp = findXOR(X);
  
            // Stores the XOR value
            arr[i][j] = temp;
        }
    }
  
    // Print resultant matrix
    prletXORmatrix(arr);
}
 
// Driver code
    let arr = [[ 27, 173, 5 ],
                    [ 21, 6, 624 ],
                    [ 5, 321, 49 ]];
  
    convertXOR(arr);;
 
// This code is contributed by susmitakundugoaldanga.
</script>
Output: 
5 5 5 
3 6 0 
5 0 13

 

Time Complexity: O(M*N*log10K) where K is the maximum element present in the matrix.
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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes




My Personal Notes arrow_drop_up
Recommended Articles
Page :