Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- int main() {
- int t;
- cin>>t;
- while(t--)
- {
- int n;
- cin >> n;
- vector <int> a(n);
- for (auto &x : a) cin >> x;
- int mmax = *max_element(a.begin(), a.end());
- vector <int> ans;
- int val = INT_MAX;
- for (int i = 0; i < 32; i++)
- {
- vector <int> b;
- int chk = -1;
- for (auto x : a)
- {
- if ((val & x) == val)
- ans.push_back(x);
- else
- {
- b.push_back(x);
- chk = max(chk, x & val);
- }
- }
- a = b;
- b.clear();
- for(auto x : a)
- {
- if ((val & x) == chk)
- {
- ans.push_back(x);
- val = chk;
- chk = -1;
- }
- else
- b.push_back(x);
- }
- a = b;
- }
- for (auto x : a) ans.push_back(x);
- val = INT_MAX;
- for (auto x : ans)
- {
- val &= x;
- cout << val << " ";
- }
- cout << "\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment