Skip to content

Commit c458cea

Browse files
authored
Create Longest Square Streak in an Array.java
1 parent 0d68682 commit c458cea

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
3+
private static final int LIMIT = 100_000;
4+
5+
public int longestSquareStreak(int[] nums) {
6+
Set<Integer> uniqueNums = new HashSet<>();
7+
for (int num : nums) {
8+
uniqueNums.add(num);
9+
}
10+
int result = 0;
11+
for (int num : nums) {
12+
int currStreak = 0;
13+
long curr = num;
14+
while (uniqueNums.contains((int) curr)) {
15+
currStreak++;
16+
if (curr * curr > LIMIT) {
17+
break;
18+
}
19+
curr *= curr;
20+
}
21+
result = Math.max(result, currStreak);
22+
}
23+
return result < 2 ? -1 : result;
24+
}
25+
}

0 commit comments

Comments
 (0)