ThreeSumFast.java
5.27 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/******************************************************************************
* Compilation: javac ThreeSumFast.java
* Execution: java ThreeSumFast input.txt
* Dependencies: StdOut.java In.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 n^2 log n running time. Reads n integers
* and counts the number of triples that sum to exactly 0.
*
* Limitations
* -----------
* - we ignore integer overflow
* - doesn't handle case when input has duplicates
*
*
* % java ThreeSumFast 1Kints.txt
* 70
*
* % java ThreeSumFast 2Kints.txt
* 528
*
* % java ThreeSumFast 4Kints.txt
* 4039
*
* % java ThreeSumFast 8Kints.txt
* 32074
*
* % java ThreeSumFast 16Kints.txt
* 255181
*
* % java ThreeSumFast 32Kints.txt
* 2052358
*
******************************************************************************/
package edu.princeton.cs.algs4;
import java.util.Arrays;
/**
* The {@code ThreeSumFast} class provides static methods for counting
* and printing the number of triples in an array of distinct integers that
* sum to 0 (ignoring integer overflow).
* <p>
* This implementation uses sorting and binary search and takes time
* proportional to n^2 log n, where n is the number of integers.
* <p>
* For additional documentation, see <a href="http://algs4.cs.princeton.edu/14analysis">Section 1.4</a> of
* <i>Algorithms, 4th Edition</i> by Robert Sedgewick and Kevin Wayne.
*
* @author Robert Sedgewick
* @author Kevin Wayne
*/
public class ThreeSumFast {
// Do not instantiate.
private ThreeSumFast() { }
// returns true if the sorted array a[] contains any duplicated integers
private static boolean containsDuplicates(int[] a) {
for (int i = 1; i < a.length; i++)
if (a[i] == a[i-1]) return true;
return false;
}
/**
* Prints to standard output the (i, j, k) with {@code i < j < k}
* such that {@code a[i] + a[j] + a[k] == 0}.
*
* @param a the array of integers
* @throws IllegalArgumentException if the array contains duplicate integers
*/
public static void printAll(int[] a) {
int n = a.length;
Arrays.sort(a);
if (containsDuplicates(a)) throw new IllegalArgumentException("array contains duplicate integers");
for (int i = 0; i < n; i++) {
for (int j = i+1; j < n; j++) {
int k = Arrays.binarySearch(a, -(a[i] + a[j]));
if (k > j) StdOut.println(a[i] + " " + a[j] + " " + a[k]);
}
}
}
/**
* Returns the number of triples (i, j, k) with {@code i < j < k}
* such that {@code a[i] + a[j] + a[k] == 0}.
*
* @param a the array of integers
* @return the number of triples (i, j, k) with {@code i < j < k}
* such that {@code a[i] + a[j] + a[k] == 0}
*/
public static int count(int[] a) {
int n = a.length;
Arrays.sort(a);
if (containsDuplicates(a)) throw new IllegalArgumentException("array contains duplicate integers");
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = i+1; j < n; j++) {
int k = Arrays.binarySearch(a, -(a[i] + a[j]));
if (k > j) count++;
}
}
return count;
}
/**
* 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.
*
* @param args the command-line arguments
*/
public static void main(String[] args) {
In in = new In(args[0]);
int[] a = in.readAllInts();
int count = count(a);
StdOut.println(count);
}
}
/******************************************************************************
* Copyright 2002-2016, Robert Sedgewick and Kevin Wayne.
*
* This file is part of algs4.jar, which accompanies the textbook
*
* Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,
* Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.
* http://algs4.cs.princeton.edu
*
*
* algs4.jar is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* algs4.jar is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with algs4.jar. If not, see http://www.gnu.org/licenses.
******************************************************************************/