Remove one element to get maximum XOR
Given an array arr[] of N elements, the task is to remove one element from the array such that the XOR value of the array is maximized. Print the maximized value.
Examples:
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
Naive approach: One way will be to remove each element one by one and then finding the XOR of the remaining elements. The time complexity of this approach will be O(N2).
Efficient approach:
- Find XOR of all the elements of the array. Let’s call this value X.
- For each element arr[i], perform Y = (X XOR arr[i]) and update the final answer as ans = max(Y, ans).
The above method works because if (A XOR B) = C then (C XOR B) = A. To find XOR(arr[0…i-1]) ^ XOR(arr[i+1…N-1]), all we have to perform is XOR(arr) ^ arr[i] where XOR(arr) is the XOR of all the elements of the array.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> 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 |
Javascript
<script> // Javascript implementation of the approach // Function to return the maximized XOR // after removing an element from the array function maxXOR(arr, n) { // Find XOR of the complete array let xorArr = 0; for (let i = 0; i < n; i++) xorArr ^= arr[i]; // To store the final answer let ans = 0; // Iterating through the array to find // the final answer for (let i = 0; i < n; i++) ans = Math.max(ans, (xorArr ^ arr[i])); // Return the final answer return ans; } // Driver code let arr = [ 1, 1, 3 ]; let n = arr.length; document.write(maxXOR(arr, n)); </script> |
2
Time Complexity: O(n)
Auxiliary Space: O(1)