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 most one 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:
C++
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int MinXOR( int N, vector< int >& A)
{
int Min_XOR = INT_MAX;
int Cum_XOR = 0;
for ( int element : A) {
Cum_XOR ^= element;
}
for ( int i = 0; i < N; i++) {
int temp = Cum_XOR ^ A[i];
Min_XOR = min(Min_XOR, temp);
}
Min_XOR = min(Min_XOR, Cum_XOR);
return Min_XOR;
}
int main()
{
int N = 5;
vector< int > A = {1, 3, 5, 17, 9};
cout << MinXOR(N, A) << endl;
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
public static void main(String[] args)
throws java.lang.Exception
{
int N = 5 ;
int [] A = { 1 , 3 , 5 , 17 , 9 };
System.out.println(MinXOR(N, A));
}
public static int MinXOR( int N, int [] A)
{
int Min_XOR = Integer.MAX_VALUE;
int Cum_XOR = 0 ;
for ( int element : A) {
Cum_XOR ^= element;
}
for ( int i = 0 ; i < N; i++) {
int temp = Cum_XOR ^ A[i];
Min_XOR = Math.min(Min_XOR, temp);
}
Min_XOR = Math.min(Min_XOR, Cum_XOR);
return Min_XOR;
}
}
|
Python3
def min_xor(n, arr):
min_xor_value = float ( 'inf' )
cum_xor = 0
for element in arr:
cum_xor ^ = element
for i in range (n):
temp = cum_xor ^ arr[i]
min_xor_value = min (min_xor_value, temp)
min_xor_value = min (min_xor_value, cum_xor)
return min_xor_value
if __name__ = = "__main__" :
N = 5
A = [ 1 , 3 , 5 , 17 , 9 ]
print (min_xor(N, A))
|
C#
using System;
using System.Collections.Generic;
class Program
{
static int MinXOR( int N, List< int > A)
{
int Min_XOR = int .MaxValue;
int Cum_XOR = 0;
foreach ( int element in A)
{
Cum_XOR ^= element;
}
for ( int i = 0; i < N; i++)
{
int temp = Cum_XOR ^ A[i];
Min_XOR = Math.Min(Min_XOR, temp);
}
Min_XOR = Math.Min(Min_XOR, Cum_XOR);
return Min_XOR;
}
static void Main( string [] args)
{
int N = 5;
List< int > A = new List< int > { 1, 3, 5, 17, 9 };
Console.WriteLine(MinXOR(N, A));
}
}
|
Javascript
function minXOR(N, A) {
let min_XOR = Number.MAX_SAFE_INTEGER;
let cum_XOR = 0;
for (let element of A) {
cum_XOR ^= element;
}
for (let i = 0; i < N; i++) {
let temp = cum_XOR ^ A[i];
min_XOR = Math.min(min_XOR, temp);
}
min_XOR = Math.min(min_XOR, cum_XOR);
return min_XOR;
}
let N = 5;
let A = [1, 3, 5, 17, 9];
console.log(minXOR(N, A));
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...