-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path2501.cpp
More file actions
24 lines (22 loc) · 694 Bytes
/
2501.cpp
File metadata and controls
24 lines (22 loc) · 694 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
class Solution {
public:
int longestSquareStreak(vector<int>& nums) {
sort(nums.begin(), nums.end());
unordered_set<int> st(nums.begin(), nums.end());
int n = nums.size();
int ans = -1;
int maximum = nums[n - 1];
for (int i = 0; i < n; ++i) {
long long start = nums[i];
if (start * start > maximum) break;
int length = 1;
while (start * start <= maximum && st.count(start * start)) {
start = start * start;
length++;
}
if (length == 1) continue;
ans = max(ans, length);
}
return ans;
}
};