Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void solve() {
- int n, m;
- cin >> n >> m;
- vector<int> a(n);
- for (int i = 0; i < n; i++)cin >> a[i];
- vector<int> check(n, 1);
- for (int i = 31; i >= 0; i--) {
- int cnt = 0;
- for (int j = 0; j < n; j++) {
- if (check[j] && ((1 << i)&a[j]))cnt++;
- }
- if (cnt >= m) {
- for (int j = 0; j < n; j++) {
- if (check[j] && ((1 << i)&a[j]) == 0)check[j] = 0;
- }
- }
- }
- int final_ans = -1;
- for (int i = 0; i < n; i++) {
- if (check[i]) {
- if (final_ans == -1)final_ans = a[i];
- final_ans &= a[i];
- }
- }
- cout << final_ans << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement