-
Notifications
You must be signed in to change notification settings - Fork 0
/
Pythagorean5.java
36 lines (33 loc) · 929 Bytes
/
Pythagorean5.java
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
import java.util.Arrays;
import java.util.HashSet;
/**
* Created by googl_000 on 11/25/2014.
*/
public class Pythagorean5 implements Pythagorean {
public int solve(int[] sequence)
{
int count = 0;
int[] squared_seq = new int[sequence.length];
// Square the array
for (int i = 0; i < squared_seq.length; i++)
{
squared_seq[i] = sequence[i] * sequence[i];
}
// Identify triples
for (int ii = 0; ii < squared_seq.length; ii++)
{
for (int jj = ii + 1; jj < squared_seq.length; jj++)
{
if (Arrays.binarySearch(squared_seq, (squared_seq[ii] + squared_seq[jj])) >= 0)
{
count++;
break;
}
}
}
return count;
}
public String getName() {
return "pre-squared with binary search";
}
}