forked from AbhirajSinha179/logicLounge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
B.cpp
66 lines (57 loc) · 1.41 KB
/
B.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include<bits/stdc++.h>
using namespace std;
void solve(){
int n;
cin >> n;
vector<int> v(n);
for (auto &ele : v) {
cin >> ele;
}
vector<int> bits(20, 0);
int low = 1;
int high = n;
int ans = n;
while (low <= high) {
set<vector<int>> checkbits;
int mid = (low + high) / 2;
fill(bits.begin(), bits.end(), 0);
for (int i = 0; i < mid; i++) {
for (int j = 0; j < 20; j++) {
if ((v[i] & (1 << j)) != 0) {
bits[j]++;
}
}
}
vector<int> temp = bits;
for (int i = 0; i < 20; i++) {
if (temp[i] >= 1) temp[i] = 1;
}
checkbits.insert(temp);
int l = 0;
int r = mid;
while (r < n) {
for (int j = 0; j < 20; j++) {
if ((v[l] & (1 << j)) != 0) bits[j]--;
if ((v[r] & (1 << j)) != 0) bits[j]++;
}
temp = bits;
for (int i = 0; i < 20; i++) {
if (temp[i] >= 1) temp[i] = 1;
}
checkbits.insert(temp);
l++;
r++;
}
if (checkbits.size() == 1) {
ans = mid;
high = mid - 1;
} else {
low = mid + 1;
}
checkbits.clear();
}
cout << ans << endl;}
int32_t main(){
solve();
return 0;
}