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++
// C++ code to implement the approach #include <bits/stdc++.h> using namespace std; // Function to find out minimum number of // element required to make array good int minNumbersRequired( int N, int arr[], int X) { // Variable to store the minimum number of // elements required int count = 0; // Map to store the frequency unordered_map< int , int > freq; freq[arr[0]]++; for ( int i = 1; i < N; i++) { // If arr[i]^X is found, // then remove one occurrence // of both the elements from the map // as their bitwise XOR is already X if (freq[arr[i] ^ X] > 0) { freq[arr[i] ^ X]--; } else freq[arr[i]]++; } // Count the number of elements remaining // in the Array as the required answer for ( auto it = freq.begin(); it != freq.end(); it++) count += it->second; // Return the minimised count of insertions return count; } // Driver Code int main() { int N = 5; int arr[] = { 1, 4, 5, 6, 7 }; int X = 7; // Function call cout << minNumbersRequired(N, arr, X); return 0; } |
Java
// Java code to implement the approach import java.io.*; import java.util.*; class GFG { // Function to find out minimum number of // element required to make array good public static int minNumbersRequired( int N, int arr[], int X) { // Variable to store the minimum number of // elements required int count = 0 ; // Map to store the frequency HashMap<Integer, Integer> freq = new HashMap<>(); freq.put(arr[ 0 ], 1 ); for ( int i = 1 ; i < N; i++) { // If arr[i]^X is found, // then remove one occurrence // of both the elements from the map // as their bitwise XOR is already X 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 ); } // Count the number of elements remaining // in the Array as the required answer for (Map.Entry<Integer, Integer> it : freq.entrySet()) count += it.getValue(); // Return the minimised count of insertions return count; } // Driver Code public static void main(String[] args) { int N = 5 ; int arr[] = { 1 , 4 , 5 , 6 , 7 }; int X = 7 ; // Function call System.out.print(minNumbersRequired(N, arr, X)); } } // This code is contributed by Rohit Pradhan |
Python3
# Python code to implement the approach # Function to find out minimum number of # element required to make array good def minNumbersRequired(N, arr, X): # Variable to store the minimum number of # elements required count = 0 # Map to store the frequency freq = {} freq[arr[ 0 ]] = 1 for i in range ( 1 ,N): # If arr[i]^X is found, # then remove one occurrence # of both the elements from the map # as their bitwise XOR is already X 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 # Count the number of elements remaining # in the Array as the required answer for it in freq: count + = freq[it] # Return the minimised count of insertions return count # Driver Code N = 5 arr = [ 1 , 4 , 5 , 6 , 7 ] X = 7 # Function call print (minNumbersRequired(N, arr, X)) # This code is contributed by shinjanpatra |
C#
// C# code to implement the approach using System; using System.Collections.Generic; public class GFG { // Function to find out minimum number of // element required to make array good public static int minNumbersRequired( int N, int [] arr, int X) { // Variable to store the minimum number of // elements required int count = 0; // Map to store the frequency Dictionary < int , int > freq = new Dictionary < int , int > (); freq[arr[0]] = 1; for ( int i = 1; i < N; i++) { // If arr[i]^X is found, // then remove one occurrence // of both the elements from the map // as their bitwise XOR is already X 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; } // Count the number of elements remaining // in the Array as the required answer foreach (KeyValuePair< int , int > it in freq) count += it.Value; // Return the minimised count of insertions return count; } public static void Main( string [] args) { int N = 5; int [] arr = { 1, 4, 5, 6, 7 }; int X = 7; // Function call Console.WriteLine(minNumbersRequired(N, arr, X)); } } // This code is contributed by phasing17 |
Javascript
<script> // JavaScript code to implement the approach // Function to find out minimum number of // element required to make array good const minNumbersRequired = (N, arr, X) => { // Variable to store the minimum number of // elements required let count = 0; // Map to store the frequency let freq = {}; freq[arr[0]] = 1; for (let i = 1; i < N; i++) { // If arr[i]^X is found, // then remove one occurrence // of both the elements from the map // as their bitwise XOR is already X 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; } } // Count the number of elements remaining // in the Array as the required answer for (let it in freq) count += freq[it]; // Return the minimised count of insertions return count; } // Driver Code let N = 5; let arr = [1, 4, 5, 6, 7]; let X = 7; // Function call document.write(minNumbersRequired(N, arr, X)); // This code is contributed by rakeshsahni </script> |
3
Time Complexity: O(N)
Auxiliary Space: O(N)
Please Login to comment...