给定非负整数的数组arr [] ,任务是找到一个整数X ,使得(arr [0] XOR X)+(arr [1] XOR X)+…+ arr [n – 1] XOR X最小可能。
例子:
Input: arr[] = {3, 9, 6, 2, 4}
Output: X = 2, Sum = 22
Input: arr[] = {6, 56, 78, 34}
Output: X = 2, Sum = 170
方法:我们将以二进制表示形式检查数组中每个数字的“ i”位,并对包含被设置为“ 1”的“ i”位的那些数字进行计数,因为这些设置位将有助于使总和最大化而不是使其最小化。因此,我们必须使这个集“第i个位为“0”,如果计数大于N / 2时,如果计数小于具有N / 2,则数字”第i个位设置较少,所以它不会影响答案。根据对两位的XOR运算,我们知道当A XOR B以及A和B都相同时,其结果为0,因此我们将数字(num)中的第i个位设为‘ 1’ ,因此(1 XOR 1)将给出‘0’并最小化总和。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
#include
using namespace std;
// Function to find an integer X such that
// the sum of all the array elements after
// getting XORed with X is minimum
void findX(int arr[], int n)
{
// Finding Maximum element of array
int* itr = max_element(arr, arr + n);
// Find Maximum number of bits required
// in the binary representation
// of maximum number
// so log2 is calculated
int p = log2(*itr) + 1;
// Running loop from p times which is
// the number of bits required to represent
// all the elements of the array
int X = 0;
for (int i = 0; i < p; i++) {
int count = 0;
for (int j = 0; j < n; j++) {
// If the bits in same position are set
// then count
if (arr[j] & (1 << i)) {
count++;
}
}
// If count becomes greater than half of
// size of array then we need to make
// that bit '0' by setting X bit to '1'
if (count > (n / 2)) {
// Again using shift operation to calculate
// the required number
X += 1 << i;
}
}
// Calculate minimized sum
long long int sum = 0;
for (int i = 0; i < n; i++)
sum += (X ^ arr[i]);
// Print solution
cout << "X = " << X << ", Sum = " << sum;
}
// Driver code
int main()
{
int arr[] = { 2, 3, 4, 5, 6 };
int n = sizeof(arr) / sizeof(arr[0]);
findX(arr, n);
return 0;
}
Java
// Java implementation of above approach
import java.lang.Math;
import java.util.*;
class GFG
{
// Function to find an integer X such that
// the sum of all the array elements after
// getting XORed with X is minimum
public static void findX(int[] a, int n)
{
// Finding Maximum element of array
Collections.sort(Arrays.asList(a), null);
int itr = a[n-1];
// Find Maximum number of bits required
// in the binary representation
// of maximum number
// so log2 is calculated
int p = (int)(Math.log(itr)/Math.log(2)) + 1;
// Running loop from p times which is
// the number of bits required to represent
// all the elements of the array
int x = 0;
for (int i = 0; i < p; i++)
{
int count = 0;
for (int j = 0; j < n; j++)
{
// If the bits in same position are set
// then count
if ((a[j] & (1 << i)) != 0)
count++;
}
// If count becomes greater than half of
// size of array then we need to make
// that bit '0' by setting X bit to '1'
if (count > (n / 2))
{
// Again using shift operation to calculate
// the required number
x += 1 << i;
}
}
// Calculate minimized sum
long sum = 0;
for (int i = 0; i < n; i++)
sum += (x ^ a[i]);
// Print solution
System.out.println("X = " + x + ", Sum = " + sum);
}
// Driver Code
public static void main(String[] args)
{
int[] a = {2, 3, 4, 5, 6};
int n = a.length;
findX(a, n);
}
}
// This code is contributed by
// sanjeev2552
Python3
# Python 3 implementation of the approach
from math import log2
# Function to find an integer X such that
# the sum of all the array elements after
# getting XORed with X is minimum
def findX(arr, n):
# Finding Maximum element of array
itr = arr[0]
for i in range(len(arr)):
# Find Maximum number of bits required
# in the binary representation
# of maximum number
# so log2 is calculated
if(arr[i] > itr):
itr = arr[i]
p = int(log2(itr)) + 1
# Running loop from p times which is
# the number of bits required to represent
# all the elements of the array
X = 0
for i in range(p):
count = 0
for j in range(n):
# If the bits in same position are set
# then increase count
if (arr[j] & (1 << i)):
count += 1
# If count becomes greater than half of
# size of array then we need to make
# that bit '0' by setting X bit to '1'
if (count > int(n / 2)):
# Again using shift operation to calculate
# the required number
X += 1 << i
# Calculate minimized sum
sum = 0
for i in range(n):
sum += (X ^ arr[i])
# Print solution
print("X =", X, ", Sum =", sum)
# Driver code
if __name__=='__main__':
arr = [2, 3, 4, 5, 6]
n = len(arr)
findX(arr, n)
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation of above approach
using System;
using System.Linq;
class GFG
{
// Function to find an integer X such that
// the sum of all the array elements after
// getting XORed with X is minimum
public static void findX(int[] a, int n)
{
// Finding Maximum element of array
int itr = a.Max();
// Find Maximum number of bits required
// in the binary representation
// of maximum number
// so log2 is calculated
int p = (int) Math.Log(itr, 2) + 1;
// Running loop from p times which is
// the number of bits required to represent
// all the elements of the array
int x = 0;
for (int i = 0; i < p; i++)
{
int count = 0;
for (int j = 0; j < n; j++)
{
// If the bits in same position are set
// then count
if ((a[j] & (1 << i)) != 0)
count++;
}
// If count becomes greater than half of
// size of array then we need to make
// that bit '0' by setting X bit to '1'
if (count > (n / 2))
{
// Again using shift operation to calculate
// the required number
x += 1 << i;
}
}
// Calculate minimized sum
long sum = 0;
for (int i = 0; i < n; i++)
sum += (x ^ a[i]);
// Print solution
Console.Write("X = " + x + ", Sum = " + sum);
}
// Driver Code
public static void Main(String[] args)
{
int[] a = {2, 3, 4, 5, 6};
int n = a.Length;
findX(a, n);
}
}
// This code is contributed by ravikishor
Javascript
输出:
X = 6, Sum = 14
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。