PROBLEM LINK: B for Bitwise | CodeChef
Problem Code: B for Bitwise | CodeChef
Practice: CodeChef | Competitive Programming | Participate & Learn | CodeChef
Contest : Carnival Finale Coding Competition | CodeChef
Author: Codechef Adgitm Chapter : tannatsri | CodeChef User Profile for Tanishq Srivastava | CodeChef
Tester: Codechef Adgitm Chapter : https://www.codechef.com/users/test_account_9
Editorialist: Codechef Adgitm Chapter : https://www.codechef.com/users/test_account_9
DIFFICULTY:
Medium
PROBLEM:
You are given an array A of N integers. Your task is to find out if there exist four distinct indices ii, jj, kk and ll such that A[i] ^ A[j] ^ A[k] ^ A[l] = 0. (Here ^ denotes the bitwise xor operation) where (0 <= i, j, k, l < n and i != j != k != l).
If there exist four such indices then print “YES” (without quotes) otherwise “NO” (without quotes).
EXPLANATION:
Traverse in array.
SOLUTION:
C++:
#include<bits/stdc++.h>
using namespace std;
int main(){
int t=0;
cin>>t;
while(t–){
int x=0;
int n=0;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
int y=a[i]^a[j];
for(int k=j+1;k<n;k++){
for(int f=k+1;f<n;f++){
int z=a[k]^a[f];
if(y==z){
x=1;
break;
}
}
if(x==1){
break;
}
}
if(x==1){
break;
}
}
if(x==1){
break;
}
}
if(x==0){
cout<<"NO"<<endl;
}
else{
cout<<"YES"<<endl;
}
}
}