Minimize insertions in Array to divide it in pairs with Bitwise XOR as X
Given an array arr of length N of distinct numbers and an integer X, the task is to find the minimum number of elements that should be added in the array such that the array elements can be grouped in pairs where the bitwise XOR of each pair is equal to X.
Examples:
Input: N = 3, arr[] = {1, 2, 3}, X = 1
Output: 1
Explanation: If we add 0 to the array then the array becomes {1, 2, 3, 0}.
This array can be split as (1, 0), (2, 3), where XOR of each pair is 1.
Input: N = 5, arr[] = {1, 4, 5, 6, 7}, X = 7
Output: 3
Explanation: If we add (2, 0, 3) to the array, it becomes [1, 4, 5, 6, 7, 2, 0, 3].
This array can be split as (1, 6), (4, 3), (5, 2), (7, 0), with XOR of each pair as 7.
Approach: The problem can be solved using properties of Bitwise XOR operator:
According to question, let us assume that each pair of Array has bitwise XOR value as X, i.e.
arr[i] ⊕ arr[j] = X
Since above statement is true, therefore below statement will also be true
arr[i] ⊕ X = arr[j]
Based on above relation, we can solve this problem as following:
- Find out existing pairs in Array with bitwise XOR value as X, and ignore them as they wont contribute to the solution.
- From the remaining elements, the bitwise XOR of each element with X will be the element to be added in the Array to satisfy the given constraint.
- Therefore count of remaining element will be the required count of insertions needed in the Array to split it into pairs with bitwise XOR as X.
Follow the steps mentioned below to implement the idea:
- Create a frequency array to store the frequency of the array elements.
- Insert the first element of the array in the hash data structure.
- Run a loop from i = 1 to N-1:
- Check if A[i] ⊕ X is already found in the array or not.
- If not found then insert the current element in the hash data structure.
- Otherwise, decrement the frequency of that element.
- Return the total number of elements which has not yet formed a pair.
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
int minNumbersRequired( int N, int arr[], int X)
{
int count = 0;
unordered_map< int , int > freq;
freq[arr[0]]++;
for ( int i = 1; i < N; i++) {
if (freq[arr[i] ^ X] > 0) {
freq[arr[i] ^ X]--;
}
else
freq[arr[i]]++;
}
for ( auto it = freq.begin(); it != freq.end(); it++)
count += it->second;
return count;
}
int main()
{
int N = 5;
int arr[] = { 1, 4, 5, 6, 7 };
int X = 7;
cout << minNumbersRequired(N, arr, X);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
public static int minNumbersRequired( int N, int arr[],
int X)
{
int count = 0 ;
HashMap<Integer, Integer> freq = new HashMap<>();
freq.put(arr[ 0 ], 1 );
for ( int i = 1 ; i < N; i++) {
if (freq.get(arr[i] ^ X) == null )
freq.put(arr[i] ^ X, 1 );
else if (freq.get(arr[i] ^ X) > 0 ) {
freq.put(arr[i] ^ X,
freq.get(arr[i] ^ X) - 1 );
}
else
freq.put(arr[i] ^ X, 1 );
}
for (Map.Entry<Integer, Integer> it :
freq.entrySet())
count += it.getValue();
return count;
}
public static void main(String[] args)
{
int N = 5 ;
int arr[] = { 1 , 4 , 5 , 6 , 7 };
int X = 7 ;
System.out.print(minNumbersRequired(N, arr, X));
}
}
|
Python3
def minNumbersRequired(N, arr, X):
count = 0
freq = {}
freq[arr[ 0 ]] = 1
for i in range ( 1 ,N):
if ((arr[i] ^ X) in freq and freq[arr[i] ^ X] > 0 ):
freq[arr[i] ^ X] - = 1
else :
if (arr[i] in freq):
freq[arr[i]] + = 1
else :
freq[arr[i]] = 1
for it in freq:
count + = freq[it]
return count
N = 5
arr = [ 1 , 4 , 5 , 6 , 7 ]
X = 7
print (minNumbersRequired(N, arr, X))
|
C#
using System;
using System.Collections.Generic;
public class GFG
{
public static int minNumbersRequired( int N, int [] arr,
int X)
{
int count = 0;
Dictionary < int , int > freq = new Dictionary < int , int > ();
freq[arr[0]] = 1;
for ( int i = 1; i < N; i++) {
if (!freq.ContainsKey(arr[i] ^ X))
freq[arr[i] ^ X] = 1;
else if (freq[arr[i] ^ X] > 0) {
freq[arr[i] ^ X] -= 1;
}
else
freq[arr[i] ^ X] = 1;
}
foreach (KeyValuePair< int , int > it in freq)
count += it.Value;
return count;
}
public static void Main( string [] args)
{
int N = 5;
int [] arr = { 1, 4, 5, 6, 7 };
int X = 7;
Console.WriteLine(minNumbersRequired(N, arr, X));
}
}
|
Javascript
<script>
const minNumbersRequired = (N, arr, X) => {
let count = 0;
let freq = {};
freq[arr[0]] = 1;
for (let i = 1; i < N; i++) {
if ((arr[i] ^ X) in freq && freq[arr[i] ^ X] > 0) {
freq[arr[i] ^ X]--;
}
else {
if (arr[i] in freq)
freq[arr[i]]++;
else freq[arr[i]] = 1;
}
}
for (let it in freq)
count += freq[it];
return count;
}
let N = 5;
let arr = [1, 4, 5, 6, 7];
let X = 7;
document.write(minNumbersRequired(N, arr, X));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
19 May, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...