XOR Pair - Solution

CoderIndeed
0

Problem : 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.

Hint

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:

 #include <bits/stdc++.h>
using namespace std;

int main() {
    //code
    int t;
    cin>>t;
    
    while(t--){
        
        int n,c;
        cin>>n>>c;
        
        int array[n];
        
        for(int i=0; i<n; i++){
            cin>>array[i];
        }
        
        unordered_set<int> s;
        
        bool flag = false;
        
        for(int i=0; i<n; i++){
            
            if(s.find(c^array[i]) != s.end()){
               flag = true;
               break;
            }
            
            s.insert(array[i]);
        }
        
        if(flag){
            cout<<"Yes"<<endl;
        }else{
            cout<<"No"<<endl;
        }
    }
    return 0;
}
 

 

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !