Advertisement
unknown_0711

Untitled

May 10th, 2023
729
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. void solve() {
  2.     int n, m;
  3.     cin >> n >> m;
  4.     vector<int> a(n);
  5.  
  6.     for (int i = 0; i < n; i++)cin >> a[i];
  7.  
  8.     vector<int> check(n, 1);
  9.  
  10.     for (int i = 31; i >= 0; i--) {
  11.         int cnt = 0;
  12.  
  13.         for (int j = 0; j < n; j++) {
  14.             if (check[j] && ((1 << i)&a[j]))cnt++;
  15.         }
  16.        
  17.         if (cnt >= m) {
  18.             for (int j = 0; j < n; j++) {
  19.                 if (check[j] && ((1 << i)&a[j]) == 0)check[j] = 0;
  20.             }
  21.         }
  22.  
  23.     }
  24.     int final_ans = -1;
  25.     for (int i = 0; i < n; i++) {
  26.         if (check[i]) {
  27.             if (final_ans == -1)final_ans = a[i];
  28.             final_ans &= a[i];
  29.         }
  30.     }
  31.     cout << final_ans << endl;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement