给定一个由N个元素组成的数组arr [] ,任务是从数组中删除一个元素,以使该数组的XOR值最大化。打印最大值。
例子:
Input: arr[] = {1, 1, 3}
Output: 2
All possible ways of deleting one element and their
corresponding XOR values will be:
a) Remove 1 -> (1 XOR 3) = 2
b) Remove 1 -> (1 XOR 3) = 2
c) Remove 3 -> (1 XOR 1) = 0
Thus, the final answer is 2.
Input: arr[] = {3, 3, 3}
Output: 0
天真的方法:一种方法是一个一个地删除每个元素,然后找到其余元素的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