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++
#include <bits/stdc++.h>
using namespace std;
const int M = 3;
const int N = 3;
int findXOR( int X)
{
int ans = 0;
while (X) {
ans ^= (X % 10);
X /= 10;
}
return ans;
}
void printXORmatrix( int arr[M][N])
{
for ( int i = 0; i < M; i++) {
for ( int j = 0; j < N; j++) {
cout << arr[i][j] << " " ;
}
cout << "\n" ;
}
}
void convertXOR( int arr[M][N])
{
for ( int i = 0; i < M; i++) {
for ( int j = 0; j < N; j++) {
int X = arr[i][j];
int temp = findXOR(X);
arr[i][j] = temp;
}
}
printXORmatrix(arr);
}
int main()
{
int arr[][3] = { { 27, 173, 5 },
{ 21, 6, 624 },
{ 5, 321, 49 } };
convertXOR(arr);
return 0;
}
|
Java
import java.io.*;
class GFG{
static int M = 3 ;
static int N = 3 ;
static int findXOR( int X)
{
int ans = 0 ;
while (X != 0 )
{
ans ^= (X % 10 );
X /= 10 ;
}
return ans;
}
static void printXORmatrix( int arr[][])
{
for ( int i = 0 ; i < M; i++)
{
for ( int j = 0 ; j < N; j++)
{
System.out.print(arr[i][j] + " " );
}
System.out.println();
}
}
static void convertXOR( int arr[][])
{
for ( int i = 0 ; i < M; i++)
{
for ( int j = 0 ; j < N; j++)
{
int X = arr[i][j];
int temp = findXOR(X);
arr[i][j] = temp;
}
}
printXORmatrix(arr);
}
public static void main (String[] args)
{
int arr[][] = { { 27 , 173 , 5 },
{ 21 , 6 , 624 },
{ 5 , 321 , 49 } };
convertXOR(arr);
}
}
|
Python3
M = 3
N = 3
def findXOR(X):
ans = 0
while (X):
ans ^ = (X % 10 )
X / / = 10
return ans
def printXORmatrix(arr):
for i in range ( 3 ):
for j in range ( 3 ):
print (arr[i][j], end = " " )
print ()
def convertXOR(arr):
for i in range ( 3 ):
for j in range ( 3 ):
X = arr[i][j]
temp = findXOR(X)
arr[i][j] = temp
printXORmatrix(arr)
if __name__ = = '__main__' :
arr = [[ 27 , 173 , 5 ],
[ 21 , 6 , 624 ],
[ 5 , 321 , 49 ]]
convertXOR(arr)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int M = 3;
static int N = 3;
static int findXOR( int X)
{
int ans = 0;
while (X != 0)
{
ans ^= (X % 10);
X /= 10;
}
return ans;
}
static void printXORmatrix( int [,] arr)
{
for ( int i = 0; i < M; i++)
{
for ( int j = 0; j < N; j++)
{
Console.Write(arr[i, j] + " " );
}
Console.WriteLine();
}
}
static void convertXOR( int [,] arr)
{
for ( int i = 0; i < M; i++)
{
for ( int j = 0; j < N; j++)
{
int X = arr[i, j];
int temp = findXOR(X);
arr[i, j] = temp;
}
}
printXORmatrix(arr);
}
static public void Main()
{
int [,] arr = { { 27, 173, 5 },
{ 21, 6, 624 },
{ 5, 321, 49 } };
convertXOR(arr);
}
}
|
Javascript
<script>
let M = 3;
let N = 3;
function findXOR(X)
{
let ans = 0;
while (X != 0)
{
ans ^= (X % 10);
X /= 10;
}
return ans;
}
function printXORmatrix(arr)
{
for (let i = 0; i < M; i++)
{
for (let j = 0; j < N; j++)
{
document.write(arr[i][j] + " " );
}
document.write( "<br/>" );
}
}
function convertXOR(arr)
{
for (let i = 0; i < M; i++)
{
for (let j = 0; j < N; j++)
{
let X = arr[i][j];
let temp = findXOR(X);
arr[i][j] = temp;
}
}
prletXORmatrix(arr);
}
let arr = [[ 27, 173, 5 ],
[ 21, 6, 624 ],
[ 5, 321, 49 ]];
convertXOR(arr);;
</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)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...