VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-minimum-bitwise-xor-by-removing-atmost-one-element/

⇱ Find Minimum Bitwise XOR By Removing Atmost One Element - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find Minimum Bitwise XOR By Removing Atmost One Element

Last Updated : 18 Jan, 2024

Given an array A[] of length N. Then your task is to output the minimum possible bitwise XOR that can be obtained by removing at mostone element.

Examples:

Input: N = 4, A[] = {2, 4, 3, 6}
Output: 0
Explanation: If we element 3 is removed from A[], then up updated A[] will be: {2, 4, 6}. Then cumulative bitwise XOR = (2^4^6) = 0. Which is minimum possible.

Input: N = 2, A[] = {1, 1}
Output: 0
Explanation: No need to remove any element. Bitwise XOR is minimum already.

Approach: We can solve this problem using below idea:

First calculate the XOR of all elements in A[] in a variable let say X. Then iterate on A[] using loop and find the minimum possible value of (X^Ai) among all iterations.

Steps were taken to solve the problem:

  • Create a variable let say Min_XOR and Cum_XOR, initialize them as Integer.MIN_VALUE and 0 respectively.
  • Run a loop to iterate A[] and Initialize cumulative XOR.
  • Run a loop from i = 0 to i < N and follow below mentioned steps under the scope of loop:
    • Temp = Cum_XOR ^ A[i]
    • Min_XOR = min(Min_XOR, temp)
  • Return Min_XOR.

Below is the implementation of the above idea:


Output
14

Time Complexity: O(N)
Auxiliary Space: O(1)

Comment