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