Modificar una array convirtiendo cada elemento a XOR de sus dígitos

Dada una array arr[][] de dimensiones M*N , la tarea es convertir cada elemento de la array en Bitwise XOR de dígitos presentes en el elemento.

Ejemplos:

Entrada: arr[][] = {{27, 173}, {5, 21}}
Salida:  
5 5
5 3
Explicación: 
Bitwise XOR de dígitos de arr[0][0] (= 27) es 5 (2^ 7).
El valor XOR bit a bit de los dígitos de arr[0][1] (= 173) es 5 (1 ^ 7 ^ 3).
El valor XOR bit a bit de los dígitos de arr[1][0] (= 5) es 5. El
valor XOR bit a bit de los dígitos de arr[1][1] (= 21) es 3(1 ^ 2).

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

Enfoque: Para resolver el problema, la idea del enfoque es atravesar la array dada y, para cada elemento de la array, imprimir el XOR bit a bit de sus dígitos .

A continuación se muestra la implementación del enfoque anterior:

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>
Producción: 

5 5 5 
3 6 0 
5 0 13

 

Complejidad Temporal: O(M*N*log 10 K) donde K es el elemento máximo presente en la array .
Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por ajaykr00kj y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *