Given an array arr[] of non-negative integers, the task is to find an integer X such that (arr[0] XOR X) + (arr[1] XOR X) + … + arr[n – 1] XOR X is minimum possible.
Examples:
Input: arr[] = {3, 9, 6, 2, 4}
Output: X = 2, Sum = 22
Input: arr[] = {6, 56, 78, 34}
Output: X = 2, Sum = 170
Approach: We will check ‘i’th bit of every number of the array in binary representation and count those numbers containing that ‘i’th bit set to ‘1’ because these set bits will contribute to maximize the sum rather than minimize. So we have to make this set ‘i’th bit to ‘0’ if the count is greater than N/2 and if the count is less than N/2 then the numbers having ‘i’th bit set are less and so it will not affect the answer. According to XOR operation on two bits, we know that when A XOR B and both A and B are the same then it gives the result as ‘0’ so we will make that ‘i’th bit in our number (num) to ‘1’, so that (1 XOR 1) will give ‘0’ and minimize the sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
void findX( int arr[], int n)
{
int * itr = max_element(arr, arr + n);
int p = log2(*itr) + 1;
int X = 0;
for ( int i = 0; i < p; i++) {
int count = 0;
for ( int j = 0; j < n; j++) {
if (arr[j] & (1 << i)) {
count++;
}
}
if (count > (n / 2)) {
X += 1 << i;
}
}
long long int sum = 0;
for ( int i = 0; i < n; i++)
sum += (X ^ arr[i]);
cout << "X = " << X << ", Sum = " << sum;
}
int main()
{
int arr[] = { 2, 3, 4, 5, 6 };
int n = sizeof (arr) / sizeof (arr[0]);
findX(arr, n);
return 0;
}
|
Java
import java.lang.Math;
import java.util.*;
class GFG
{
public static void findX( int [] a, int n)
{
Collections.sort(Arrays.asList(a), null );
int itr = a[n- 1 ];
int p = ( int )(Math.log(itr)/Math.log( 2 )) + 1 ;
int x = 0 ;
for ( int i = 0 ; i < p; i++)
{
int count = 0 ;
for ( int j = 0 ; j < n; j++)
{
if ((a[j] & ( 1 << i)) != 0 )
count++;
}
if (count > (n / 2 ))
{
x += 1 << i;
}
}
long sum = 0 ;
for ( int i = 0 ; i < n; i++)
sum += (x ^ a[i]);
System.out.println( "X = " + x + ", Sum = " + sum);
}
public static void main(String[] args)
{
int [] a = { 2 , 3 , 4 , 5 , 6 };
int n = a.length;
findX(a, n);
}
}
|
Python3
from math import log2
def findX(arr, n):
itr = arr[ 0 ]
for i in range ( len (arr)):
if (arr[i] > itr):
itr = arr[i]
p = int (log2(itr)) + 1
X = 0
for i in range (p):
count = 0
for j in range (n):
if (arr[j] & ( 1 << i)):
count + = 1
if (count > int (n / 2 )):
X + = 1 << i
sum = 0
for i in range (n):
sum + = (X ^ arr[i])
print ( "X =" , X, ", Sum =" , sum )
if __name__ = = '__main__' :
arr = [ 2 , 3 , 4 , 5 , 6 ]
n = len (arr)
findX(arr, n)
|
C#
using System;
using System.Linq;
class GFG
{
public static void findX( int [] a, int n)
{
int itr = a.Max();
int p = ( int ) Math.Log(itr, 2) + 1;
int x = 0;
for ( int i = 0; i < p; i++)
{
int count = 0;
for ( int j = 0; j < n; j++)
{
if ((a[j] & (1 << i)) != 0)
count++;
}
if (count > (n / 2))
{
x += 1 << i;
}
}
long sum = 0;
for ( int i = 0; i < n; i++)
sum += (x ^ a[i]);
Console.Write( "X = " + x + ", Sum = " + sum);
}
public static void Main(String[] args)
{
int [] a = {2, 3, 4, 5, 6};
int n = a.Length;
findX(a, n);
}
}
|
Javascript
<script>
function findX(arr, n)
{
let itr = Math.max(...arr);
let p = parseInt(Math.log(itr) / Math.log(2)) + 1;
let X = 0;
for (let i = 0; i < p; i++) {
let count = 0;
for (let j = 0; j < n; j++) {
if (arr[j] & (1 << i)) {
count++;
}
}
if (count > parseInt(n / 2)) {
X += 1 << i;
}
}
let sum = 0;
for (let i = 0; i < n; i++)
sum += (X ^ arr[i]);
document.write( "X = " + X + ", Sum = " + sum);
}
let arr = [ 2, 3, 4, 5, 6 ];
let n = arr.length;
findX(arr, n);
</script>
|
Time Complexity: O(N * log(A))
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
02 Aug, 2021
Like Article
Save Article