programming-examples/java/Data_Structures/ThreeSum.java
2019-11-15 12:59:38 +01:00

101 lines
3.3 KiB
Java

import edu.princeton.cs.introcs.In;
import edu.princeton.cs.introcs.StdOut;
/*************************************************************************
* Compilation: javac ThreeSum.java
* Execution: java ThreeSum input.txt
* Dependencies: In.java StdOut.java Stopwatch.java
* Data files: http://algs4.cs.princeton.edu/14analysis/1Kints.txt
* http://algs4.cs.princeton.edu/14analysis/2Kints.txt
* http://algs4.cs.princeton.edu/14analysis/4Kints.txt
* http://algs4.cs.princeton.edu/14analysis/8Kints.txt
* http://algs4.cs.princeton.edu/14analysis/16Kints.txt
* http://algs4.cs.princeton.edu/14analysis/32Kints.txt
* http://algs4.cs.princeton.edu/14analysis/1Mints.txt
*
* A program with cubic running time. Read in N integers
* and counts the number of triples that sum to exactly 0
* (ignoring integer overflow).
*
* % java ThreeSum 1Kints.txt
* 70
*
* % java ThreeSum 2Kints.txt
* 528
*
* % java ThreeSum 4Kints.txt
* 4039
*
*************************************************************************/
/**
* The ThreeSum class provides static methods for counting
* and printing the number of triples in an array of integers that sum to 0
* (ignoring integer overflow).
*
* This implementation uses a triply nested loop and takes proportional to N^3,
* where N is the number of integers.
*
* For additional documentation, see <a href="http://algs4.cs.princeton.edu/14analysis">Section 1.4</a> of
* Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class ThreeSum {
/**
* Prints to standard output the (i, j, k) with i < j < k such that a[i] + a[j] + a[k] == 0.
* @param a the array of integers
*/
public static void printAll(int[] a) {
int N = a.length;
for (int i = 0; i < N; i++) {
for (int j = i+1; j < N; j++) {
for (int k = j+1; k < N; k++) {
if (a[i] + a[j] + a[k] == 0) {
StdOut.println(a[i] + " " + a[j] + " " + a[k]);
}
}
}
}
}
/**
* Returns the number of triples (i, j, k) with i < j < k such that a[i] + a[j] + a[k] == 0.
* @param a the array of integers
* @return the number of triples (i, j, k) with i < j < k such that a[i] + a[j] + a[k] == 0
*/
public static int count(int[] a) {
int N = a.length;
int cnt = 0;
for (int i = 0; i < N; i++) {
for (int j = i+1; j < N; j++) {
for (int k = j+1; k < N; k++) {
if (a[i] + a[j] + a[k] == 0) {
cnt++;
}
}
}
}
return cnt;
}
/**
* 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.
*/
public static void main(String[] args) {
In in = new In(args[0]);
int[] a = in.readAllInts();
Stopwatch timer = new Stopwatch();
int cnt = count(a);
StdOut.println("elapsed time = " + timer.elapsedTime());
StdOut.println(cnt);
}
}