-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsolution.java
More file actions
37 lines (25 loc) · 840 Bytes
/
Copy pathsolution.java
File metadata and controls
37 lines (25 loc) · 840 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
25
26
27
28
29
30
31
32
33
34
35
36
37
class Solution {
boolean pythagoreanTriplet(int[] arr) {
int maxVal = 0;
// Find maximum value
for (int num : arr)
maxVal = Math.max(maxVal, num);
boolean[] present = new boolean[maxVal + 1];
for (int num : arr)
present[num] = true;
// Try every pair (a, b)
for (int a = 1; a <= maxVal; a++) {
if (!present[a])
continue;
for (int b = a; b <= maxVal; b++) {
if (!present[b])
continue;
int cSquare = a * a + b * b;
int c = (int) Math.sqrt(cSquare);
if (c <= maxVal && c * c == cSquare && present[c])
return true;
}
}
return false;
}
}