-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathThreeSumFast.h
84 lines (77 loc) · 2.65 KB
/
ThreeSumFast.h
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
#ifndef CH1_THREESUMFAST_H
#define CH1_THREESUMFAST_H
#include <vector>
#include <iostream>
#include <algorithm>
#include <stdexcept>
using std::vector;
using std::cout;
using std::endl;
using std::sort;
using std::binary_search;
using std::runtime_error;
/**
* 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="https://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
*/
class ThreeSumFast {
public:
ThreeSumFast() = delete;
// returns true if the sorted array a[] contains any duplicated integers
static bool containsDuplicates(vector<int> &a) {
for (int i = 1; i < a.size(); 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
*/
static void printAll(vector<int> &a) {
int n = a.size();
sort(a.begin(), a.end());
if (containsDuplicates(a)) throw runtime_error("array contains duplicate integers");
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
bool k = binary_search(a.begin() + j, a.end(), -(a[i] + a[j]));
if (k)
cout << a[i] << " " << a[j] << " " << -(a[i] + a[j]) << endl;
}
}
}
/**
* 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}
*/
static int count(vector<int> &a) {
int n = a.size();
sort(a.begin(), a.end());
if (containsDuplicates(a)) throw runtime_error("array contains duplicate integers");
int count = 0;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
bool k = binary_search(a.begin() + j, a.end(), -(a[i] + a[j]));
if (k) count++;
}
}
return count;
}
};
#endif //CH1_THREESUMFAST_H