-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFourSum.java
More file actions
35 lines (33 loc) · 1.02 KB
/
FourSum.java
File metadata and controls
35 lines (33 loc) · 1.02 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
public class FourSum{
public static int count(int[] a) {
int n = a.length;
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = i+1; j < n; j++) {
for (int k = j+1; k < n; k++) {
for(int h = k+1; h < n; h++){
if (a[i] + a[j] + a[k] + a[h] == 0) {
count++;
}
}
}
}
}
return count;
}
/**
* Reads in a sequence of integers from a file, specified as a command-line argument;
* counts the number of triples sum to exactly zero; prints out the time to perform
* the computation.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
In in = new In(args[0]);
int[] a = in.readAllInts();
Stopwatch timer = new Stopwatch();
int count = count(a);
StdOut.println("elapsed time = " + timer.elapsedTime());
StdOut.println(count);
}
}