-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path3020.cpp
More file actions
38 lines (38 loc) · 1.07 KB
/
Copy path3020.cpp
File metadata and controls
38 lines (38 loc) · 1.07 KB
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
class Solution {
public:
int maximumLength(vector<int>& nums) {
map<long long, int> mp;
for (auto num : nums) mp[num] += 1;
int n = mp.size();
vector<long long> elements(n, 0);
int i = 0;
for (auto& [k, v] : mp) {
elements[i] = k;
i++;
}
int res = 1;
for (int i = 0; i < n; ++i) {
long long element = elements[i];
if (element == 1) {
int count = mp[element];
if (!(count & 1)) count -= 1;
res = max(res, count);
continue;
}
if (mp[element] == -1) continue;
if (mp[element] <= 1) {
continue;
}
int curr = 1;
while (mp.count(element * element)) {
element = element * element;
curr++;
int count = mp[element];
mp[element] = -1;
if (count <= 1) break;
}
res = max(res, curr * 2 - 1);
}
return res;
}
};