Bitwise XOR de Bitwise AND de todos los pares de dos arrays dadas

Dadas dos arrays arr1[] y arr2[] que constan de N y M enteros respectivamente, la tarea es imprimir el Bitwise XOR de Bitwise AND de todos los pares posibles seleccionando un elemento de arr1[] y arr2[].

Ejemplos:

Entrada: arr1[] = {1, 2, 3}, arr2[] = {6, 5}
Salida: 0
Explicación: 
AND bit a bit del par (arr1[0], arr2[]) = 1 y 6 = 0.
AND bit a bit del par (arr1[0], arr2[1]) = 1 & 5 = 1.
AND bit a bit del par (arr1[1], arr2[0]) = 2 & 6 = 2.
AND bit a bit del par par (arr1[1], arr2[1]) = 2 y 5 = 0.
AND bit a bit del par (arr1[2], arr2[0]) = 3 & 6 = 2.
AND bit a bit del par (arr1[ 2], arr2[1]) = 3 & 5 = 1.
Por lo tanto, el Bitwise XOR de los valores Bitwise AND obtenidos = 0 ^ 1 ^ 2 ^ 0^ 2 ^ 1 = 0.

Entrada: arr1[] = {12}, arr2[] = {4}
Salida: 4

Enfoque ingenuo: el enfoque más simple es encontrar el AND bit a bit de todos los pares posibles seleccionando un elemento de arr1[] y otro elemento de arr2[] y luego, calculando el XOR bit a bit de todos los AND bit a bit de los pares resultantes.

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;
 
// Function to find the Bitwise XOR
// of Bitwise AND of all pairs from
// the arrays arr1[] and arr2[]
int findXORS(int arr1[], int arr2[], int N, int M)
{
    // Stores the result
    int res = 0;
 
    // Iterate over the range [0, N - 1]
    for (int i = 0; i < N; i++) {
 
        // Iterate over the range [0, M - 1]
        for (int j = 0; j < M; j++) {
 
            // Stores Bitwise AND of
            // the pair {arr1[i], arr2[j]}
            int temp = arr1[i] & arr2[j];
 
            // Update res
            res ^= temp;
        }
    }
    // Return the res
    return res;
}
 
// Driver Code
int main()
{
    // Input
    int arr1[] = { 1, 2, 3 };
    int arr2[] = { 6, 5 };
    int N = sizeof(arr1) / sizeof(arr1[0]);
    int M = sizeof(arr2) / sizeof(arr2[0]);
 
    cout << findXORS(arr1, arr2, N, M);
 
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function to find the Bitwise XOR
// of Bitwise AND of all pairs from
// the arrays arr1[] and arr2[]
static int findXORS(int arr1[], int arr2[],
                    int N, int M)
{
     
    // Stores the result
    int res = 0;
 
    // Iterate over the range [0, N - 1]
    for(int i = 0; i < N; i++)
    {
         
        // Iterate over the range [0, M - 1]
        for(int j = 0; j < M; j++)
        {
             
            // Stores Bitwise AND of
            // the pair {arr1[i], arr2[j]}
            int temp = arr1[i] & arr2[j];
 
            // Update res
            res ^= temp;
        }
    }
     
    // Return the res
    return res;
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Input
    int arr1[] = { 1, 2, 3 };
    int arr2[] = { 6, 5 };
    int N = arr1.length;
    int M = arr2.length;
 
    System.out.print(findXORS(arr1, arr2, N, M));
}
}
 
// This code is contributed by 29AjayKumar

Python3

# Python 3 program for the above approach
 
# Function to find the Bitwise XOR
# of Bitwise AND of all pairs from
# the arrays arr1[] and arr2[]
def findXORS(arr1, arr2, N, M):
   
    # Stores the result
    res = 0
 
    # Iterate over the range [0, N - 1]
    for i in range(N):
       
        # Iterate over the range [0, M - 1]
        for j in range(M):
            # Stores Bitwise AND of
            # the pair {arr1[i], arr2[j]}
            temp = arr1[i] & arr2[j]
 
            # Update res
            res ^= temp
    # Return the res
    return res
 
# Driver Code
if __name__ == '__main__':
   
    # Input
    arr1 = [1, 2, 3]
    arr2 = [6, 5]
    N = len(arr1)
    M = len(arr2)
    print(findXORS(arr1, arr2, N, M))
     
    # This code is contributed by ipg2016107.

C#

// C# program for the above approach
using System;
class GFG
{
   
    // Function to find the Bitwise XOR
    // of Bitwise AND of all pairs from
    // the arrays arr1[] and arr2[]
    static int findXORS(int[] arr1, int[] arr2, int N,
                        int M)
    {
        // Stores the result
        int res = 0;
 
        // Iterate over the range [0, N - 1]
        for (int i = 0; i < N; i++) {
 
            // Iterate over the range [0, M - 1]
            for (int j = 0; j < M; j++)
            {
 
                // Stores Bitwise AND of
                // the pair {arr1[i], arr2[j]}
                int temp = arr1[i] & arr2[j];
 
                // Update res
                res ^= temp;
            }
        }
       
        // Return the res
        return res;
    }
 
    // Driver Code
    public static void Main()
    {
       
        // Input
        int[] arr1 = { 1, 2, 3 };
        int[] arr2 = { 6, 5 };
        int N = arr1.Length;
        int M = arr2.Length;
 
        Console.Write(findXORS(arr1, arr2, N, M));
    }
}
 
// This code is contributed by ukasp.

Javascript

<script>
// Javascript program for the above approach
 
 
// Function to find the Bitwise XOR
// of Bitwise AND of all pairs from
// the arrays arr1[] and arr2[]
function findXORS(arr1, arr2, N, M) {
    // Stores the result
    let res = 0;
 
    // Iterate over the range [0, N - 1]
    for (let i = 0; i < N; i++) {
 
        // Iterate over the range [0, M - 1]
        for (let j = 0; j < M; j++) {
 
            // Stores Bitwise AND of
            // the pair {arr1[i], arr2[j]}
            let temp = arr1[i] & arr2[j];
 
            // Update res
            res ^= temp;
        }
    }
    // Return the res
    return res;
}
 
// Driver Code
 
// Input
let arr1 = [1, 2, 3];
let arr2 = [6, 5];
let N = arr1.length;
let M = arr2.length;
 
document.write(findXORS(arr1, arr2, N, M));
 
// This code is contributed by _saurabh_jaiswal
</script>
Producción: 

0

 

Complejidad de Tiempo: O(N * M)
Espacio Auxiliar: O(1)

Enfoque eficiente: el enfoque anterior se puede optimizar en función de las siguientes observaciones: 

  • Las operaciones Bitwise Xor y Bitwise And tienen propiedades aditivas y distributivas.
  • Por lo tanto, considerando las arrays como arr1[] = {A, B} y arr2[] = {X, Y}:
    • (A Y X) XOR (A Y Y) XOR (B Y X) XOR (B Y Y)
    • (A Y (X XOR Y)) XOR (B Y (X XOR Y))
    • (A X O B) Y (X X O Y)
  • Por lo tanto, a partir de los pasos anteriores, la tarea se reduce a encontrar el Y bit a bit del XOR bit a bit de arr1[] y arr2[].

Siga los pasos a continuación para resolver el problema:

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;
 
// Function to find the Bitwise XOR
// of Bitwise AND of all pairs from
// the arrays arr1[] and arr2[]
int findXORS(int arr1[], int arr2[],
             int N, int M)
{
    // Stores XOR of array arr1[]
    int XORS1 = 0;
 
    // Stores XOR of array arr2[]
    int XORS2 = 0;
 
    // Traverse the array arr1[]
    for (int i = 0; i < N; i++) {
        XORS1 ^= arr1[i];
    }
 
    // Traverse the array arr2[]
    for (int i = 0; i < M; i++) {
        XORS2 ^= arr2[i];
    }
 
    // Return the result
    return XORS1 and XORS2;
}
 
// Driver Code
int main()
{
    // Input
    int arr1[] = { 1, 2, 3 };
    int arr2[] = { 6, 5 };
    int N = sizeof(arr1) / sizeof(arr1[0]);
    int M = sizeof(arr2) / sizeof(arr2[0]);
 
    cout << findXORS(arr1, arr2, N, M);
 
    return 0;
}

Java

// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG{
 
// Function to find the Bitwise XOR
// of Bitwise AND of all pairs from
// the arrays arr1[] and arr2[]
static int findXORS(int arr1[], int arr2[],
                    int N, int M)
{
     
    // Stores XOR of array arr1[]
    int XORS1 = 0;
  
    // Stores XOR of array arr2[]
    int XORS2 = 0;
  
    // Traverse the array arr1[]
    for(int i = 0; i < N; i++)
    {
        XORS1 ^= arr1[i];
    }
  
    // Traverse the array arr2[]
    for(int i = 0; i < M; i++)
    {
        XORS2 ^= arr2[i];
    }
  
    // Return the result
    return (XORS1 & XORS2);
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Input
    int arr1[] = { 1, 2, 3 };
    int arr2[] = { 6, 5 };
    int N = arr1.length;
    int M = arr2.length;
     
    System.out.println(findXORS(arr1, arr2, N, M));
}
}
 
// This code is contributed by susmitakundugoaldanga

Python3

# Python3 program for the above approach
 
# Function to find the Bitwise XOR
# of Bitwise AND of all pairs from
# the arrays arr1[] and arr2[]
def findXORS(arr1, arr2, N, M):
     
    # Stores XOR of array arr1[]
    XORS1 = 0
 
    # Stores XOR of array arr2[]
    XORS2 = 0
 
    # Traverse the array arr1[]
    for i in range(N):
        XORS1 ^= arr1[i]
 
    # Traverse the array arr2[]
    for i in range(M):
        XORS2 ^= arr2[i]
 
    # Return the result
    return XORS1 and XORS2
 
# Driver Code
if __name__ == '__main__':
     
    # Input
    arr1 = [ 1, 2, 3 ]
    arr2 = [ 6, 5 ]
    N = len(arr1)
    M = len(arr2)
     
    print(findXORS(arr1, arr2, N, M))
 
# This code is contributed by bgangwar59

C#

// C# program for the above approach
using System;
 
class GFG{
 
// Function to find the Bitwise XOR
// of Bitwise AND of all pairs from
// the arrays arr1[] and arr2[]
static int findXORS(int []arr1, int []arr2,
                    int N, int M)
{
     
    // Stores XOR of array arr1[]
    int XORS1 = 0;
  
    // Stores XOR of array arr2[]
    int XORS2 = 0;
  
    // Traverse the array arr1[]
    for(int i = 0; i < N; i++)
    {
        XORS1 ^= arr1[i];
    }
  
    // Traverse the array arr2[]
    for(int i = 0; i < M; i++)
    {
        XORS2 ^= arr2[i];
    }
  
    // Return the result
    return (XORS1 & XORS2);
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Input
    int []arr1 = { 1, 2, 3 };
    int []arr2 = { 6, 5 };
    int N = arr1.Length;
    int M = arr2.Length;
     
    Console.WriteLine(findXORS(arr1, arr2, N, M));
}
}
 
// This code is contributed by 29AjayKumar

Javascript

<script>
// JavaScript program for the above approach
 
// Function to find the Bitwise XOR
// of Bitwise AND of all pairs from
// the arrays arr1[] and arr2[]
function findXORS(arr1, arr2, N, M)
{
    // Stores XOR of array arr1[]
    let XORS1 = 0;
 
    // Stores XOR of array arr2[]
    let XORS2 = 0;
 
    // Traverse the array arr1[]
    for (let i = 0; i < N; i++) {
        XORS1 ^= arr1[i];
    }
 
    // Traverse the array arr2[]
    for (let i = 0; i < M; i++) {
        XORS2 ^= arr2[i];
    }
 
    // Return the result
    return XORS1 && XORS2;
}
 
// Driver Code
 
    // Input
    let arr1 = [ 1, 2, 3 ];
    let arr2 = [ 6, 5 ];
    let N = arr1.length;
    let M = arr2.length;
 
    document.write(findXORS(arr1, arr2, N, M));
     
       // This code is contributed by Dharanendra L V.
 
</script>
Producción: 

0

 

Complejidad temporal: O(N + M)
Espacio auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por srinivasteja18 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 *