Bitwise XOR of same indexed array elements after rearranging an array to make XOR of same indexed elements of two arrays equal
Last Updated :
13 Sep, 2022
Given two arrays A[] and B[] consisting of N positive integers, the task is to the Bitwise XOR of same indexed array elements after rearranging the array B[] such that the Bitwise XOR of the same indexed elements of the arrays A[] becomes equal.
Examples:
Input: A[] = {1, 2, 3}, B[] = {4, 6, 7}
Output: 5
Explanation:
Below are the possible arrangements:
- Rearrange the array B[] to {4, 7, 6}. Now, the Bitwise XOR of the same indexed elements are equal, i.e. 1 ^ 4 = 5, 2 ^ 7 = 5, 3 ^ 6 = 5.
After the rearrangements, required Bitwise XOR is 5.
Input: A[] = { 7, 8, 14 }, B[] = { 5, 12, 3 }
Output: 11
Explanation:
Below are the possible arrangements:
- Rearrange the array B[] to {12, 3, 5}. Now, the Bitwise XOR of the same indexed elements are equal, i.e. 7 ^ 12 = 11, 8 ^ 3 = 11, 14 ^ 5 = 11.
After the rearrangements, required Bitwise XOR is 11.
Naive Approach: The given problem can be solved based on the observation that the count of rearrangements can be at most N because any element in A[] can only be paired with N other integers in B[]. So, there are N candidate values for X. Now, simply XOR all the candidates with each element in A[] and check if B[] can be arranged in that order.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void findPossibleValues( int A[], int B[],
int n)
{
sort(B, B + n);
int C[n];
set< int > numbers;
for ( int i = 0; i < n; i++) {
int candidate = A[0] ^ B[i];
for ( int j = 0; j < n; j++) {
C[j] = A[j] ^ candidate;
}
sort(C, C + n);
bool flag = false ;
for ( int j = 0; j < n; j++)
if (C[j] != B[j])
flag = true ;
if (!flag)
numbers.insert(candidate);
}
for ( auto x : numbers) {
cout << x << " " ;
}
}
int main()
{
int A[] = { 7, 8, 14 };
int B[] = { 5, 12, 3 };
int N = sizeof (A) / sizeof (A[0]);
findPossibleValues(A, B, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void findPossibleValues( int A[], int B[],
int n)
{
Arrays.sort(B);
int []C = new int [n];
HashSet<Integer> numbers = new HashSet<Integer>();
for ( int i = 0 ; i < n; i++) {
int candidate = A[ 0 ] ^ B[i];
for ( int j = 0 ; j < n; j++) {
C[j] = A[j] ^ candidate;
}
Arrays.sort(C);
boolean flag = false ;
for ( int j = 0 ; j < n; j++)
if (C[j] != B[j])
flag = true ;
if (!flag)
numbers.add(candidate);
}
for ( int x : numbers) {
System.out.print(x+ " " );
}
}
public static void main(String[] args)
{
int A[] = { 7 , 8 , 14 };
int B[] = { 5 , 12 , 3 };
int N = A.length;
findPossibleValues(A, B, N);
}
}
|
Python3
def findPossibleValues(A, B, n):
B.sort();
C = [ 0 ] * n;
numbers = set ();
for i in range (n):
candidate = A[ 0 ] ^ B[i];
for j in range (n):
C[j] = A[j] ^ candidate;
C.sort();
flag = False ;
for j in range (n):
if (C[j] ! = B[j]):
flag = True ;
if ( not flag):
numbers.add(candidate);
for x in numbers:
print (x, end = " " );
A = [ 7 , 8 , 14 ];
B = [ 5 , 12 , 3 ];
N = len (A);
findPossibleValues(A, B, N);
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static void findPossibleValues( int []A, int []B,
int n)
{
Array.Sort(B);
int []C = new int [n];
HashSet< int > numbers = new HashSet< int >();
for ( int i = 0; i < n; i++) {
int candidate = A[0] ^ B[i];
for ( int j = 0; j < n; j++) {
C[j] = A[j] ^ candidate;
}
Array.Sort(C);
bool flag = false ;
for ( int j = 0; j < n; j++)
if (C[j] != B[j])
flag = true ;
if (!flag)
numbers.Add(candidate);
}
foreach ( int x in numbers) {
Console.Write(x+ " " );
}
}
public static void Main(String[] args)
{
int []A = { 7, 8, 14 };
int []B = { 5, 12, 3 };
int N = A.Length;
findPossibleValues(A, B, N);
}
}
|
Javascript
<script>
function findPossibleValues(A, B, n) {
B.sort((a, b) => a - b);
let C = new Array(n);
let numbers = new Set();
for (let i = 0; i < n; i++) {
let candidate = A[0] ^ B[i];
for (let j = 0; j < n; j++) {
C[j] = A[j] ^ candidate;
}
C.sort((a, b) => a - b);
let flag = false ;
for (let j = 0; j < n; j++) if (C[j] != B[j]) flag = true ;
if (!flag) numbers.add(candidate);
}
for (let x of numbers) {
document.write(x + " " );
}
}
let A = [7, 8, 14];
let B = [5, 12, 3];
let N = A.length;
findPossibleValues(A, B, N);
</script>
|
Time Complexity: O(N2*log(N))
Auxiliary Space: O(N)
Efficient Approach: The above approach can also be optimized by not sorting the array and store the bit-wise-XOR of all elements of B[], and then the Bitwise XOR with all elements in C[]. Now if the result is 0 then it means both arrays have same elements. Follow the steps below to solve the problem:
- Initialize the variable, say x that stores the XOR of all the elements of the array B[].
- Initialize a set, say numbers[] to store only unique numbers..
- Iterate over the range [0, N) using the variable i and perform the following steps:
- Initialize the variables candidate as the XOR of A[0] and B[i] and curr_xor as x to see if it is 0 after performing the requires operations.
- Iterate over the range [0, N) using the variable j and perform the following steps:
- Initialize the variable y as the XOR of A[j] and candidate and XOR y with curr_xor.
- If curr_xor is equal to 0, then insert the value candidate into the set numbers[].
- After performing the above steps, print the set numbers[] as the answer.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void findPossibleValues( int A[], int B[],
int n)
{
int x = 0;
for ( int i = 0; i < n; i++) {
x = x ^ B[i];
}
set< int > numbers;
for ( int i = 0; i < n; i++) {
int candidate = A[0] ^ B[i];
int curr_xor = x;
for ( int j = 0; j < n; j++) {
int y = A[j] ^ candidate;
curr_xor = curr_xor ^ y;
}
if (curr_xor == 0)
numbers.insert(candidate);
}
for ( auto x : numbers) {
cout << x << " " ;
}
}
int main()
{
int A[] = { 7, 8, 14 };
int B[] = { 5, 12, 3 };
int N = sizeof (A) / sizeof (A[0]);
findPossibleValues(A, B, N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findPossibleValues( int A[], int B[],
int n)
{
int x = 0 ;
for ( int i = 0 ; i < n; i++) {
x = x ^ B[i];
}
HashSet<Integer> numbers = new HashSet<Integer>();
for ( int i = 0 ; i < n; i++) {
int candidate = A[ 0 ] ^ B[i];
int curr_xor = x;
for ( int j = 0 ; j < n; j++) {
int y = A[j] ^ candidate;
curr_xor = curr_xor ^ y;
}
if (curr_xor == 0 )
numbers.add(candidate);
}
for ( int i : numbers) {
System.out.print(i + " " );
}
}
public static void main(String[] args)
{
int A[] = { 7 , 8 , 14 };
int B[] = { 5 , 12 , 3 };
int N = A.length;
findPossibleValues(A, B, N);
}
}
|
Python3
def findPossibleValues(A, B, n):
x = 0
for i in range (n):
x = x ^ B[i]
numbers = set ()
for i in range (n):
candidate = A[ 0 ] ^ B[i]
curr_xor = x
for j in range (n):
y = A[j] ^ candidate
curr_xor = curr_xor ^ y
if (curr_xor = = 0 ):
numbers.add(candidate)
for x in numbers:
print (x, end = " " )
if __name__ = = '__main__' :
A = [ 7 , 8 , 14 ]
B = [ 5 , 12 , 3 ]
N = len (A)
findPossibleValues(A, B, N)
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
static void findPossibleValues( int []A, int []B,
int n)
{
int x = 0;
for ( int i = 0; i < n; i++) {
x = x ^ B[i];
}
HashSet< int > numbers = new HashSet< int >();
for ( int i = 0; i < n; i++) {
int candidate = A[0] ^ B[i];
int curr_xor = x;
for ( int j = 0; j < n; j++) {
int y = A[j] ^ candidate;
curr_xor = curr_xor ^ y;
}
if (curr_xor == 0)
numbers.Add(candidate);
}
foreach ( int i in numbers) {
Console.Write(i + " " );
}
}
public static void Main(String[] args)
{
int []A = { 7, 8, 14 };
int []B = { 5, 12, 3 };
int N = A.Length;
findPossibleValues(A, B, N);
}
}
|
Javascript
<script>
function findPossibleValues(A, B, n)
{
var x = 0;
for ( var i = 0; i < n; i++) {
x = x ^ B[i];
}
var numbers = new Set();
for ( var i = 0; i < n; i++) {
var candidate = A[0] ^ B[i];
var curr_xor = x;
for ( var j = 0; j < n; j++) {
var y = A[j] ^ candidate;
curr_xor = curr_xor ^ y;
}
if (curr_xor == 0)
numbers.add(candidate);
}
for ( var i of numbers) {
document.write(i + " " );
}
}
var A = [ 7, 8, 14 ];
var B = [ 5, 12, 3 ];
var N = A.length;
findPossibleValues(A, B, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(N) as using set “numbers”
Share your thoughts in the comments
Please Login to comment...