-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLC2501.java
More file actions
57 lines (42 loc) · 1.31 KB
/
LC2501.java
File metadata and controls
57 lines (42 loc) · 1.31 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/*
* LC2501
*/
import java.util.*;
public class LC2501 {
public static int longestSquareStreak(int[] nums) {
TreeSet<Integer> sortedSet = new TreeSet<>();
for (int num : nums) {
sortedSet.add(num);
}
HashSet<Integer> numSet = new HashSet<>(sortedSet);
int maxLength = 0;
for (int num : sortedSet) {
int length = 0;
long current = num;
while (current <= Integer.MAX_VALUE && numSet.contains((int) current)) {
length++;
current = current * current;
}
if (length > 1) {
maxLength = Math.max(maxLength, length);
}
}
return maxLength > 1 ? maxLength : -1;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Array Size : ");
int size = sc.nextInt();
System.out.println();
int[] arr = new int[size];
System.out.println("Enter the Array Elements : ");
for (int i = 0; i < arr.length; i++) {
System.out.printf("[%d] : ", i);
arr[i] = sc.nextInt();
}
System.out.println();
int ans = longestSquareStreak(arr);
System.out.println(ans);
sc.close();
}
}