Challenges - Bitmasking -> Unique 2
Here’s my code :
#include<iostream>
using namespace std;
void solve(int n,int A[],int ans){
int tmp=ans,pos=0;
while((tmp&1)==0){
tmp>>=1;
pos++;
}
int mask = 1<<pos;
int Set1 = 0;
for(int i=0;i<n;i++){
if((A[i]&mask)>0)
Set1^=A[i];
}
int Set2 = ans^Set1;
cout<<Set1<<" "<<Set2<<endl;
}
void XOR(int n,int A[]){
int ans=0;
for(int i=0;i<n;i++){
ans^=A[i];
}
// cout<<ans;
solve(n,A,ans);
}
int main(){
int n;
cin>>n;
int A[n+1];
for(int i=0;i<n;i++){
cin>>A[i];
}
XOR(n,A);
return 0;
}
Though, it is giving the right answer for the input
4
3 1 2 1
output:
3 2
Yet declared wrong answer when submitted for test1 and test2.