-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanipulative-numbers.cpp
63 lines (51 loc) · 1.48 KB
/
manipulative-numbers.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
#include <iostream>
#include <vector>
#include <map>
#include <algorithm>
using namespace std;
const int MAX_N = 101;
int elements[MAX_N];
int num_elements;
// Function to check if there exists a permutation with M(B) >= 2^k
bool isKManipulative(int k) {
map<int, int> countMap;
int mask = 0;
// Create a mask to consider only the top k bits
for (int i = 30; i >= k; i--) {
mask |= (1 << i);
}
// Count occurrences of masked elements
for (int i = 0; i < num_elements; i++) {
countMap[elements[i] & mask]++;
}
vector<int> counts;
for (const auto& pair : countMap) {
counts.push_back(pair.second);
}
// Sort counts to check the condition for being k-manipulative
sort(counts.begin(), counts.end());
if (counts.size() == 1) return false;
int cumulative_sum = counts[0];
for (size_t i = 1; i < counts.size(); i++) {
if (cumulative_sum < counts[i]) return false;
cumulative_sum += counts[i];
}
return true;
}
int main() {
// Read input
cin >> num_elements;
for (int i = 0; i < num_elements; i++) {
cin >> elements[i];
}
// Find the largest k for which there exists a k-manipulative permutation
for (int k = 30; k >= 0; k--) {
if (isKManipulative(k)) {
cout << k << endl;
return 0;
}
}
// If no valid k found, output -1 (though by constraints, we should always find a valid k)
cout << -1 << endl;
return 0;
}