Given an array arr[] consisting of N integers, the task is to find an integer K, not having more than maximum bits present in any array element, which when added to the array, maximizes the Bitwise XOR of the array.
Examples:
Input: N = 7, arr[] = {1, 7, 8, 11, 6, 9, 6}
Output: 3
Explanation:
Bitwise XOR of the given array = 1 ^ 7 ^ 8 ^ 11 ^ 6 ^ 9 ^ 6 = 12
(12)2 = (1100)2
(0011)2 = (3)10 is the best option of maximizing XOR of the array.
Therefore, 12 ^ 3 = 15 is the maximum possible XOR of the given array.Input: N = 5, arr[] = {1, 2, 3, 4, 5}
Output: 6
Explanation:
Bitwise XOR of the given array = 1 ^ 2 ^ 3 ^ 4 ^ 5 = 1
(1)2 = (0001)2
(0110)2 = (6)10 is the best option of maximizing XOR of the array.
Therefore, 1 ^ 6 = 7 is the maximum possible XOR of the given array.
Approach: Follow the steps below to solve the problem:
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 complement of an integer unsigned int onesComplement(unsigned int n, int maxElement) { // Count the number of bits of maxElement int bits = floor(log2(maxElement)) + 1; // Return 1's complement return ((1 << bits) - 1) ^ n; } // Function to find the value required to be // added to maximize XOR of the given array int findNumber(int arr[], int n) { // Stores the required value // to be added to the array unsigned int res = 0; // Stores the maximum array element int maxElement = 0; // Traverse the array for (int i = 0; i < n; i++) { // Update XOR of the array res = res ^ arr[i]; // Find maximum element in array if (maxElement < arr[i]) maxElement = arr[i]; } // Calculate 1s' complement res = onesComplement(res, maxElement); // Return the answer return (res); } // Driver Code int main() { int arr[] = { 1, 2, 3, 4, 5 }; int N = sizeof(arr) / sizeof(arr[0]); cout << findNumber(arr, N); return 0; } |
6
Time Complexity: O(N)
Auxiliary Space: O(1)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Calculate Bitwise OR of two integers from their given Bitwise AND and Bitwise XOR values
- Total pairs in an array such that the bitwise AND, bitwise OR and bitwise XOR of LSB is 1
- Rearrange given binary strings to maximize their Bitwise XOR value
- Count pairs with bitwise XOR exceeding bitwise AND from a given array
- Maximize sum of squares of array elements possible by replacing pairs with their Bitwise AND and Bitwise OR
- Queries to minimize sum added to given ranges in an array to make their Bitwise AND non-zero
- Minimum value by which each Array element must be added as per given conditions
- Leftover element after performing alternate Bitwise OR and Bitwise XOR operations on adjacent pairs
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Find the minimum value to be added so that array becomes balanced
- Minimum value to be added to the prefix sums at each array indices to make them positive
- Queries to update each element in subarray to Bitwise XOR with a given value
- Minimum possible Bitwise OR of all Bitwise AND of pairs generated from two given arrays
- Count pairs with equal Bitwise AND and Bitwise OR value
- Minimum value to be added to X such that it is at least Y percent of N
- Minimum value of X that can be added to N to minimize sum of the digits to ≤ K
- Minimum Bitwise XOR operations to make any two array elements equal
- Largest possible value of M not exceeding N having equal Bitwise OR and XOR between them
- Minimize elements to be added to a given array such that it contains another given array as its subsequence
- Minimize elements to be added to a given array such that it contains another given array as its subsequence | Set 2
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

