Codechef September long challenge 2021 | XOR Equal Solution

 

Codechef September long challenge 2021 | XOR Equal Solution

You are given an array AA consisting of NN integers and an integer XX. Your goal is to have as many equal integers as possible in the array. To achieve this goal, you can do the following operation:

  • Choose an index ii (1iN)(1≤i≤N) and set Ai=AiXAi=Ai⊕X, where  denotes the bitwise xor operation.

Find the maximum number of equal integers you can have in the final array and the minimum number of operations to obtain these many equal integers.

Input Format

  • The first line of the input contains a single integer TT denoting the number of test cases. The description of TT test cases follows.
  • Each test case contains two lines of input.
  • The first line of each test case contains two space-separated integers N,XN,X.
  • The second line of each test case contains NN space-separated integers A1,A2,,ANA1,A2,…,AN.

Output Format

For each test case, print a single line containing two space-separated integers – first, the maximum number of equal integers in the final array and second, the minimum number of operations to achieve these many equal integers.

Constraints

  • 1T1041≤T≤104
  • 1N1051≤N≤105
  • 0X1090≤X≤109
  • 0Ai1090≤Ai≤109
  • The sum of NN over all test cases does not exceed 51055⋅105.

Subtasks

Subtask #1 (100 points): Original constraints

Sample Input 1 

3
3 2
1 2 3
5 100
1 2 3 4 5
4 1
2 2 6 6

Sample Output 1 

2 1
1 0
2 0

 

C++ Solution

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
int main() {
int a;
cin>>a;
while(a--){
int n,x,p;
cin>>n>>x;
unordered_map<int,int> mp;
unordered_map<int,int> present;
for(int i=0;i<n;i++){
cin>>p;
mp[p]++;
present[p]=1;
}
if(n==1){
cout<< 1 << " " << 0 << endl;
continue;
}
int ans=0;
int changes = 0;
for(auto it: mp){
if(it.second==n){
ans=n;
break;
}
if(it.second>=ans){
ans=it.second;
}
}
if(x==0){
cout<< ans << " " << changes << endl;
continue;
}
for(auto it:mp){
if(present[(it.first)^x]==1){
if(it.second + mp[(it.first)^x]>ans){
ans = it.second + mp[(it.first)^x];
changes = min(it.second,mp[(it.first)^x]);
}
else if(it.second + mp[(it.first)^x]==ans){
if(min(it.second,mp[(it.first)^x]) < changes){
changes = min(it.second, mp[(it.first)^x]);
}
}
}
}
cout<< ans << " " << changes <<endl;
}
return 0;
}

 

 

Post a Comment

Previous Post Next Post