XOR Pair: Given an array of positive element having size N and an integer C. Check if there exists a pair (A,B) such that A xor B = C.
Input :
First line of input contains number of testcases T. The 1st line of each testcase contains a two integers N and C. The 2nd line of each testcase, contains N space separated integers denoting the elements of the array A.
Output:
Print “Yes” is the pair exists else print “No” without quotes.(Change line after every answer).
Constraints:
1 <= T <= 100
1 <= N <= 105
1 <= C <= 105
0 <= arr[i] <= 105
Example:
Input:
2
7 7
2 1 10 3 4 9 5
5 1
9 9 10 10 3
Output:
Yes
No
Explanation :
In first case, pair (2,5) give 7. Hence answer is “Yes”. In second case no pair exist such that satisfies the condition hance the answer is “No”.
Solution:
import java.util.*;
import java.lang.*;
import java.io.*;
public class XORPair {
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
String next(){
while (st == null || !st.hasMoreElements()){
try {
st = new StringTokenizer(br.readLine());
}catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt(){
return Integer.parseInt(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader fr = new FastReader();
int t = fr.nextInt();
while (t-->0){
int n = fr.nextInt();
int c = fr.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = fr.nextInt();
}
HashSet<Integer> set = new HashSet<>();
int r = 0;
for (int i = 0; i < n; i++) {
if (set.contains(arr[i]^c)){
r+=1;
}
set.add(arr[i]);
}
if (r>0){
System.out.println("Yes");
}else {
System.out.println("No");
}
}
}
}
#include <bits/stdc++. h>
using namespace std;
int main() {
//
int t;
cin>>t;
while(t--){
int n,c;
cin>>n>>c;
int arr[n];
for(int i=0; i<n; i++){
cin>>arr[i];
}
unordered_set<int> s;
bool flag = false;
for(int i=0; i<n; i++){
if(s.find(c^arr[i]) != s.end()){
flag = true;
break;
}
s.insert(arr[i]);
}
if(flag){
cout<<"Yes"<<endl;
}else{
cout<<"No"<<endl;
}
}
return 0;
}
Happy Learning – If you require any further information, feel free to contact me.