Bitwise XOR of Bitwise AND of all pairs from two given arrays

Given two arrays arr1[] and arr2[] consisting of N and M integers respectively, the task is to print the Bitwise XOR of Bitwise AND of all pairs possible by selecting an element from arr1[] and arr2[].
Examples:
Input: arr1[] = {1, 2, 3}, arr2[] = {6, 5}
Output: 0
Explanation:Â
Bitwise AND of the pair (arr1[0], arr2[]) = 1 & 6 = 0.
Bitwise AND of the pair (arr1[0], arr2[1]) = 1 & 5 = 1.
Bitwise AND of the pair (arr1[1], arr2[0]) = 2 & 6 = 2.
Bitwise AND of the pair (arr1[1], arr2[1]) = 2 & 5 = 0.
Bitwise AND of the pair (arr1[2], arr2[0]) = 3 & 6 = 2.
Bitwise AND of the pair (arr1[2], arr2[1]) = 3 & 5 = 1.
Therefore, the Bitwise XOR of the obtained Bitwise AND values = 0 ^ 1 ^ 2 ^ 0^ 2 ^ 1 = 0.Input: arr1[] = {12}, arr2[] = {4}
Output: 4
Naive Approach: The simplest approach is to find Bitwise AND of all possible pairs possible by selecting an element from arr1[] and another element from arr2[] and then, calculating the Bitwise XOR of all Bitwise AND of resultant pairs.
Below is the implementation of the above approach:
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 Codeint 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 approachimport 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 Codepublic 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 Codeif __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 approachusing 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Â
// Inputlet 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> |
0
Â
Time Complexity: O(N * M)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized based on the following observations:Â
- The Bitwise Xor and Bitwise And operation have Additive and Distributive properties.
- Therefore, considering the arrays as arr1[] = {A, B} and arr2[] = {X, Y}:
- (A AND X) XOR (A AND Y) XOR (B AND X) XOR (B AND Y)
- (A AND ( X XOR Y)) XOR (B AND ( X XOR Y))
- (A XOR B) AND (X XOR Y)
- Hence, from the above steps, the task is reduced to finding the bitwise And of bitwise XOR of arr1[] and arr2[].
Follow the steps below to solve the problem:
- Find the bitwise Xor of every array element of an array arr1[] and store it in a variable, say XORS1.
- Find the bitwise Xor of every array element of an array arr2[] and store it in a variable, say XORS2.
- Finally, print the result as bitwise AND of XORS1 and XORS2.
Below is the implementation of the above approach:
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 Codeint 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 approachimport 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 Codepublic 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 Codeif __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 approachusing 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 Codepublic 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> |
0
Â
Time Complexity: O(N + M)
Auxiliary Space: O(1)
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



