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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

2

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.