-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTwoSum_MiS.java
More file actions
108 lines (101 loc) · 2.81 KB
/
TwoSum_MiS.java
File metadata and controls
108 lines (101 loc) · 2.81 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
import java.util.Arrays;
//import edu.princeton.cs.algs4.In;
//import edu.princeton.cs.algs4.Stopwatch;
/******************************************************************************
* Compilation: javac TwoSum.java
* Execution: java TwoSum 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 running time. Reads n integers
* and counts the number of pairs that sum to exactly 0.
*
*
* Limitations
* -----------
* - we ignore integer overflow
*
*
* % java TwoSum 2Kints.txt
* 2
*
* % java TwoSum 1Kints.txt
* 1
*
* % java TwoSum 2Kints.txt
* 2
*
* % java TwoSum 4Kints.txt
* 3
*
* % java TwoSum 8Kints.txt
* 19
*
* % java TwoSum 16Kints.txt
* 66
*
* % java TwoSum 32Kints.txt
* 273
*
******************************************************************************/
public class TwoSum_MiS {
// print distinct pairs (i, j) such that a[i] + a[j] = 0
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++) {
if (a[i] + a[j] == 0) {
StdOut.println(a[i] + " " + a[j]);
}
}
}
}
public static int binarySearch(int[] a, int key) {
int beg = 0;
int end = a.length - 1;
while (beg <= end) {
// Key is in a[beg..end] or not present.
int mid = beg + (end - beg) / 2;
if(key < a[mid]) end = mid - 1;
else if(key > a[mid]) beg = mid + 1;
else return mid;
}
return -1;
}
public static int count(int[] a) {
Arrays.sort(a);
int n = a.length;
int count = 0;
for (int i = 0; i < n; i++) {
if(a[i]>0)
{
if(binarySearch(a,-a[i]) >= 0)
{
count++;
}
}
}
return count;
}
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);
}
}
//Output
/*
Milos@Milos-PC MINGW64 ~/Documents/+PHS/Algo (master)
$ java TwoSum_MiS 8Kints.txt
Elapsed time = 0.005
19
*/