Ankit_132

D

May 29th, 2024 (edited)
716
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.36 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5.     int t;
  6.     cin>>t;
  7.    
  8.     while(t--)
  9.     {
  10.         int n;
  11.         cin >> n;
  12.         vector <int> a(n);
  13.        
  14.         for (auto &x : a) cin >> x;
  15.    
  16.         int mmax = *max_element(a.begin(), a.end());
  17.    
  18.         vector <int> ans;
  19.         int val = INT_MAX;
  20.        
  21.         for (int i = 0; i < 32; i++)
  22.         {
  23.             vector <int> b;
  24.             int chk = -1;
  25.            
  26.             for (auto x : a)
  27.             {
  28.                 if ((val & x) == val)
  29.                     ans.push_back(x);
  30.                 else
  31.                 {
  32.                     b.push_back(x);
  33.                     chk = max(chk, x & val);
  34.                 }
  35.             }
  36.    
  37.             a = b;
  38.             b.clear();
  39.             for(auto x : a)
  40.             {
  41.                 if ((val & x) == chk)
  42.                 {
  43.                     ans.push_back(x);
  44.                     val = chk;
  45.                     chk = -1;
  46.                 }
  47.                 else
  48.                     b.push_back(x);
  49.                
  50.             }
  51.            
  52.             a = b;
  53.         }
  54.    
  55.         for (auto x : a) ans.push_back(x);
  56.        
  57.         val = INT_MAX;
  58.         for (auto x : ans)
  59.         {
  60.             val &= x;
  61.             cout << val << " ";
  62.         }
  63.         cout << "\n";
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment