📌  相关文章
📜  删除一个元素以获得最大的XOR

📅  最后修改于: 2021-04-29 15:10:50             🧑  作者: Mango

给定一个由N个元素组成的数组arr [] ,任务是从数组中删除一个元素,以使该数组的XOR值最大化。打印最大值。

例子:

天真的方法:一种方法是一个一个地删除每个元素,然后找到其余元素的XOR。该方法的时间复杂度将为O(N 2 )。

高效的方法:

  • 查找数组中所有元素的XOR。我们将此值称为X。
  • 对于每个元素arr [i] ,执行Y =(X XOR arr [i])并将最终答案更新为ans = max(Y,ans)

上面的方法之所以有效,是因为如果(A XOR B)= C,那么(C XOR B)=A。要找到XOR(arr [0…i-1])^ XOR(arr [i + 1…N-1]) ,我们要做的就是XOR(arr)^ arr [i] ,其中XOR(arr)是数组所有元素的XOR。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the maximized XOR
// after removing an element from the array
int maxXOR(int* arr, int n)
{
    // Find XOR of the complete array
    int xorArr = 0;
    for (int i = 0; i < n; i++)
        xorArr ^= arr[i];
  
    // To store the final answer
    int ans = 0;
  
    // Iterating through the array to find
    // the final answer
    for (int i = 0; i < n; i++)
        ans = max(ans, (xorArr ^ arr[i]));
  
    // Return the final answer
    return ans;
}
  
// Driver code
int main()
{
    int arr[] = { 1, 1, 3 };
    int n = sizeof(arr) / sizeof(int);
  
    cout << maxXOR(arr, n);
  
    return 0;
}


Java
// Java implementation of the approach 
class GFG
{
      
    // Function to return the maximized XOR 
    // after removing an element from the array 
    static int maxXOR(int arr[], int n) 
    { 
        // Find XOR of the complete array 
        int xorArr = 0; 
        for (int i = 0; i < n; i++) 
            xorArr ^= arr[i]; 
      
        // To store the final answer 
        int ans = 0; 
      
        // Iterating through the array to find 
        // the final answer 
        for (int i = 0; i < n; i++) 
            ans = Math.max(ans, (xorArr ^ arr[i])); 
      
        // Return the final answer 
        return ans; 
    } 
      
    // Driver code 
    public static void main (String[] args) 
    { 
        int arr[] = { 1, 1, 3 }; 
        int n = arr.length; 
        System.out.println(maxXOR(arr, n)); 
    } 
}
  
// This code is contributed by AnkitRai01


Python3
# Python3 implementation of the approach
  
# Function to return the maximized XOR
# after removing an element from the array
def maxXOR(arr, n):
      
    # Find XOR of the complete array
    xorArr = 0
    for i in range(n):
        xorArr ^= arr[i]
  
    # To store the final answer
    ans = 0
  
    # Iterating through the array to find
    # the final answer
    for i in range(n):
        ans = max(ans, (xorArr ^ arr[i]))
  
    # Return the final answer
    return ans
  
# Driver code
arr = [1, 1, 3]
n = len(arr)
  
print(maxXOR(arr, n))
  
# This code is contributed by Mohit Kumar


C#
// C# implementation of the approach 
using System;
  
class GFG
{
      
    // Function to return the maximized XOR 
    // after removing an element from the array 
    static int maxXOR(int []arr, int n) 
    { 
        // Find XOR of the complete array 
        int xorArr = 0; 
        for (int i = 0; i < n; i++) 
            xorArr ^= arr[i]; 
      
        // To store the readonly answer 
        int ans = 0; 
      
        // Iterating through the array to find 
        // the readonly answer 
        for (int i = 0; i < n; i++) 
            ans = Math.Max(ans, (xorArr ^ arr[i])); 
      
        // Return the readonly answer 
        return ans; 
    } 
      
    // Driver code 
    public static void Main(String[] args) 
    { 
        int []arr = { 1, 1, 3 }; 
        int n = arr.Length; 
        Console.WriteLine(maxXOR(arr, n)); 
    } 
}
  
// This code is contributed by 29AjayKumar


输出:
2