926E Merge Equal Elements
The problem can be found here
My Thought Process
The first thing that we might try doing is to keep another vector and add elements from the given array to our newly created vector. Afterwards, we can repeately look at the back of the vector and check if two elements are equal. That’s actually all there is to the problem!
This runs in $O(n)$ time.
Implementation
#include <bits/stdc++.h>
using ll = long long;
#define pb push_back
using namespace std;
const int MOD = 1e9+7;
const int MAXN = 2e6+1;
const int INF = 2e9;
const long long IINF = 2e18;
#define int long long
void solve() {
int n;
cin >> n;
vector<int> a(n);
for(int i = 0; i < n; ++i){cin >> a[i];}
vector<int> b(n);
for(int i = 0; i < n; ++i){cin >> b[i];}
int ans = 0;
for(int i = 30; i >= 0; --i){
int bit = (1 << i);
map<int, int> ca;
map<int, int> cb;
for(int j = 0; j < n; ++j){
ca[a[j] & (bit | ans)]++;
cb[b[j] & (bit | ans)]++;
}
bool flag = true;
for(auto j : ca){
if(j.second != cb[(bit | ans) ^ j.first]){
flag = false;
break;
}
}
if(flag){
ans += bit;
}
}
cout << ans << endl;
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
// freopen("test.in", "r", stdin);
// freopen("test.out", "w", stdout);
int t;
cin >> t;
while(t--){
solve();
}
return 0;
}
Final Thoughts
This was way easier than the typically 1900 problem. Probably because only $65$ people solved it during contest and it was an older problem.