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++
#include <bits/stdc++.h>
using namespace std;
int findXORS( int arr1[], int arr2[], int N, int M)
{
int res = 0;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < M; j++) {
int temp = arr1[i] & arr2[j];
res ^= temp;
}
}
return res;
}
int main()
{
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
import java.util.*;
class GFG{
static int findXORS( int arr1[], int arr2[],
int N, int M)
{
int res = 0 ;
for ( int i = 0 ; i < N; i++)
{
for ( int j = 0 ; j < M; j++)
{
int temp = arr1[i] & arr2[j];
res ^= temp;
}
}
return res;
}
public static void main(String[] args)
{
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));
}
}
|
Python3
def findXORS(arr1, arr2, N, M):
res = 0
for i in range (N):
for j in range (M):
temp = arr1[i] & arr2[j]
res ^ = temp
return res
if __name__ = = '__main__' :
arr1 = [ 1 , 2 , 3 ]
arr2 = [ 6 , 5 ]
N = len (arr1)
M = len (arr2)
print (findXORS(arr1, arr2, N, M))
|
C#
using System;
class GFG
{
static int findXORS( int [] arr1, int [] arr2, int N,
int M)
{
int res = 0;
for ( int i = 0; i < N; i++) {
for ( int j = 0; j < M; j++)
{
int temp = arr1[i] & arr2[j];
res ^= temp;
}
}
return res;
}
public static void Main()
{
int [] arr1 = { 1, 2, 3 };
int [] arr2 = { 6, 5 };
int N = arr1.Length;
int M = arr2.Length;
Console.Write(findXORS(arr1, arr2, N, M));
}
}
|
Javascript
<script>
function findXORS(arr1, arr2, N, M) {
let res = 0;
for (let i = 0; i < N; i++) {
for (let j = 0; j < M; j++) {
let temp = arr1[i] & arr2[j];
res ^= temp;
}
}
return res;
}
let arr1 = [1, 2, 3];
let arr2 = [6, 5];
let N = arr1.length;
let M = arr2.length;
document.write(findXORS(arr1, arr2, N, M));
</script>
|
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:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findXORS( int arr1[], int arr2[],
int N, int M)
{
int XORS1 = 0;
int XORS2 = 0;
for ( int i = 0; i < N; i++) {
XORS1 ^= arr1[i];
}
for ( int i = 0; i < M; i++) {
XORS2 ^= arr2[i];
}
return XORS1 and XORS2;
}
int main()
{
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
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static int findXORS( int arr1[], int arr2[],
int N, int M)
{
int XORS1 = 0 ;
int XORS2 = 0 ;
for ( int i = 0 ; i < N; i++)
{
XORS1 ^= arr1[i];
}
for ( int i = 0 ; i < M; i++)
{
XORS2 ^= arr2[i];
}
return (XORS1 & XORS2);
}
public static void main(String[] args)
{
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));
}
}
|
Python3
def findXORS(arr1, arr2, N, M):
XORS1 = 0
XORS2 = 0
for i in range (N):
XORS1 ^ = arr1[i]
for i in range (M):
XORS2 ^ = arr2[i]
return XORS1 and XORS2
if __name__ = = '__main__' :
arr1 = [ 1 , 2 , 3 ]
arr2 = [ 6 , 5 ]
N = len (arr1)
M = len (arr2)
print (findXORS(arr1, arr2, N, M))
|
C#
using System;
class GFG{
static int findXORS( int []arr1, int []arr2,
int N, int M)
{
int XORS1 = 0;
int XORS2 = 0;
for ( int i = 0; i < N; i++)
{
XORS1 ^= arr1[i];
}
for ( int i = 0; i < M; i++)
{
XORS2 ^= arr2[i];
}
return (XORS1 & XORS2);
}
public static void Main(String[] args)
{
int []arr1 = { 1, 2, 3 };
int []arr2 = { 6, 5 };
int N = arr1.Length;
int M = arr2.Length;
Console.WriteLine(findXORS(arr1, arr2, N, M));
}
}
|
Javascript
<script>
function findXORS(arr1, arr2, N, M)
{
let XORS1 = 0;
let XORS2 = 0;
for (let i = 0; i < N; i++) {
XORS1 ^= arr1[i];
}
for (let i = 0; i < M; i++) {
XORS2 ^= arr2[i];
}
return XORS1 && XORS2;
}
let arr1 = [ 1, 2, 3 ];
let arr2 = [ 6, 5 ];
let N = arr1.length;
let M = arr2.length;
document.write(findXORS(arr1, arr2, N, M));
</script>
|
Time Complexity: O(N + M)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
28 Jun, 2021
Like Article
Save Article