Check this out

this question is from Practice Coding Problem (codechef.com) this contest and I came up with a solution for this question , before reading the code let me clarify my approach and why i coded this solution, as we know xor of two same numbers is zero so i first thought of making as much zeroes as possible by choosing X such that it is the maximum occuring element as it will make maximum elements of the array as zero which will eventually minimise the bitwise OR of the array, is something fishy in my approach i don’t know,i don’t have any proof why my solutions works but it does i tried to figure out why this works by checking out others solution but all have done the same thing that is calculating OR of whole array and using that as X if anyone knows this kindly explain it to me that why it works, thanks in advance.
#include<bits/stdc++.h>
using namespace std;
define ll long long
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin>>t;
while(t–){
int n;
cin>>n;
vectorarr(n);
map<int,int>mpp;
for(int i=0;i<n;i++){
cin>>arr[i];
mpp[arr[i]]++;
}
int maxi=INT_MIN;
int ans;
for(auto x: mpp){
if(x.second>maxi){
maxi=x.second;
ans=x.first;
}
}
cout<<ans<<" ";
for(int i=0;i<n;i++){
arr[i]^=ans;
}
int orr=0;
for(int i=0;i<n;i++)
{
orr|=arr[i];
}
cout<<orr<<endl;

}

return 0;
}

to minimize the or we have to find the bit that are set for all array element
then we set that bit for x
thats the minimum value of x
but you can take x as any array element the answer will be correct

can you elaborate more please
@vanshwari

If all the elements of A has i bit 0.
then x should have i bit 0
if all elements of A has i bit 1
then x should have i bit 1;
if some elments have i bit 1 or some another has it 0
then x can have bit i as either 1 or 0

1 Like

you can check out XORORED - Editorial

1 Like