Minimum value to be added to maximize Bitwise XOR of the given array
Last Updated :
13 Sep, 2021
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:
- Calculate the Bitwise XOR of all the elements of the array
- Calculate the complement of calculated XOR of the array elements such that the number of bits in the complement is equal to the maximum bits present in any array element.
- The complement of XOR is the required value to be added to maximize the Bitwise XOR of the given array.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
unsigned int onesComplement(unsigned int n,
int maxElement)
{
int bits = floor (log2(maxElement)) + 1;
return ((1 << bits) - 1) ^ n;
}
int findNumber( int arr[], int n)
{
unsigned int res = 0;
int maxElement = 0;
for ( int i = 0; i < n; i++) {
res = res ^ arr[i];
if (maxElement < arr[i])
maxElement = arr[i];
}
res = onesComplement(res,
maxElement);
return (res);
}
int main()
{
int arr[] = { 1, 2, 3, 4, 5 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << findNumber(arr, N);
return 0;
}
|
Java
import java.util.*;
import java.io.*;
import java.lang.*;
class GFG{
static int onesComplement( int n,
int maxElement)
{
int bits = ( int )Math.floor((
Math.log(maxElement) /
Math.log( 2 ))) + 1 ;
return (( 1 << bits) - 1 ) ^ n;
}
static int findNumber( int arr[], int n)
{
int res = 0 ;
int maxElement = 0 ;
for ( int i = 0 ; i < n; i++)
{
res = res ^ arr[i];
if (maxElement < arr[i])
maxElement = arr[i];
}
res = onesComplement(res,
maxElement);
return (res);
}
public static void main(String[] args)
{
int arr[] = { 1 , 2 , 3 , 4 , 5 };
int N = arr.length;
System.out.print(findNumber(arr, N));
}
}
|
Python3
import math
def onesComplement(n, maxElement) :
bits = math.floor(math.log2(maxElement)) + 1
return (( 1 << bits) - 1 ) ^ n
def findNumber(arr, n) :
res = 0
maxElement = 0
for i in range (n):
res = res ^ arr[i]
if (maxElement < arr[i]):
maxElement = arr[i]
res = onesComplement(res, maxElement)
return (res)
arr = [ 1 , 2 , 3 , 4 , 5 ]
N = len (arr)
print (findNumber(arr, N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int onesComplement( int n,
int maxElement)
{
int bits = ( int )Math.Floor((
Math.Log(maxElement) /
Math.Log(2))) + 1 ;
return ((1 << bits) - 1) ^ n;
}
static int findNumber( int [] arr, int n)
{
int res = 0;
int maxElement = 0;
for ( int i = 0; i < n; i++)
{
res = res ^ arr[i];
if (maxElement < arr[i])
maxElement = arr[i];
}
res = onesComplement(res,
maxElement);
return (res);
}
public static void Main()
{
int [] arr = { 1, 2, 3, 4, 5 };
int N = arr.Length;
Console.Write(findNumber(arr, N));
}
}
|
Javascript
<script>
function onesComplement(n,
maxElement)
{
let bits = Math.floor((
Math.log(maxElement) /
Math.log(2))) + 1 ;
return ((1 << bits) - 1) ^ n;
}
function findNumber(arr, n)
{
let res = 0;
let maxElement = 0;
for (let i = 0; i < n; i++)
{
res = res ^ arr[i];
if (maxElement < arr[i])
maxElement = arr[i];
}
res = onesComplement(res,
maxElement);
return (res);
}
let arr = [ 1, 2, 3, 4, 5 ];
let N = arr.length;
document.write(findNumber(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...