public class ThreeSumFast extends java.lang.Object
This implementation uses sorting and binary search and takes time proportional to N^2 log N, where N is the number of integers.
For additional documentation, see Section 1.4 of Algorithms, 4th Edition, by Robert Sedgewick and Kevin Wayne.
Constructor and Description |
---|
ThreeSumFast() |
Modifier and Type | Method and Description |
---|---|
static int |
count(int[] a)
Returns the number of triples (i, j, k) with i < j < k such that a[i] + a[j] + a[k] == 0.
|
static void |
main(java.lang.String[] args)
Reads in a sequence of distinct 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.
|
static void |
printAll(int[] a)
Prints to standard output the (i, j, k) with i < j < k such that a[i] + a[j] + a[k] == 0.
|
public static void printAll(int[] a)
a
- the array of integersjava.lang.IllegalArgumentException
- if the array contains duplicate integerspublic static int count(int[] a)
a
- the array of integerspublic static void main(java.lang.String[] args)