📌  相关文章
📜  要添加的最小值,以使给定阵列的按位XOR最大化

📅  最后修改于: 2021-05-13 23:49:52             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是找到一个整数K ,其位数不超过任何数组元素中存在的最大位数,将其添加到数组后,可使数组的按位XOR最大化。

例子:

方法:请按照以下步骤解决问题:

  • 计算数组所有元素的按位XOR
  • 计算数组元素的已计算XOR数,以使补数中的位数等于任何数组元素中存在的最大位数。
  • XOR的补码是要添加的值,以使给定数组的按位XOR最大化。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
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;
}


Java
// Java program for the above approach
import java.util.*;
import java.io.*;
import java.lang.*;
 
class GFG{
  
// Function to find complement of an integer
static int onesComplement(int n,
                          int maxElement)
{
     
    // Count the number of bits of maxElement
    int bits = (int)Math.floor((
                    Math.log(maxElement) /
                    Math.log(2))) + 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
static int findNumber(int arr[], int n)
{
     
    // Stores the required value
    // to be added to the array
    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
public static void main(String[] args)
{
    int arr[] = { 1, 2, 3, 4, 5 };
    int N = arr.length;
     
    System.out.print(findNumber(arr, N));
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python program for the above approach
import math
  
# Function to find complement of an integer
def onesComplement(n, maxElement) :
     
    # Count the number of bits of maxElement
    bits = math.floor(math.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
def findNumber(arr, n) :
     
    # Stores the required value
    # to be added to the array
    res = 0
  
    # Stores the maximum array element
    maxElement = 0
  
    # Traverse the array
    for i in range(n):
  
        # 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
 
arr = [ 1, 2, 3, 4, 5 ]
N = len(arr)
print(findNumber(arr, N))
 
# This code is contributed by code_hunt.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
   
// Function to find complement of an integer
static int onesComplement(int n,
                          int maxElement)
{
     
    // Count the number of bits of maxElement
    int bits = (int)Math.Floor((
                    Math.Log(maxElement) /
                    Math.Log(2))) + 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
static int findNumber(int[] arr, int n)
{
     
    // Stores the required value
    // to be added to the array
    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
public static void Main()
{
    int[] arr = { 1, 2, 3, 4, 5 };
    int N = arr.Length;
      
    Console.Write(findNumber(arr, N));
}
}
 
// This code is contributed by susmitakundugoaldanga


Javascript


输出:
6

时间复杂度: O(N)
辅助空间: O(1)