-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwoSum.java
65 lines (60 loc) · 2.12 KB
/
TwoSum.java
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
/***************************************************************
*
* Author: Daria Shubina
* Date: 15/03/09
* Compilation: javac TwoSum.java
* Execution: java -ea TwoSum
* Description: Given an integer K and an array of integers,
* find how many combination of two integers
* (not necessarily distinct) sum up to K.
* A[i] + A[j] = K: (i, j) and (j, i)
* count as two combinations.
* A[i] + A[i] = K: (i, i) count as a one.
*
/***************************************************************/
import java.util.Arrays;
public class TwoSum {
public static int solution(int K, int[] A) {
int n = A.length;
if (n == 0) return 0;
Arrays.sort(A);
int[] values = new int[n];
int[] counts = new int[n];
values[0] = A[0];
counts[0] = 1;
int j = 0; //counter of unique elements
for (int i = 1; i < n; ++i) {
if (A[i] != values[j]) values[++j] = A[i];
++counts[j];
}
int lo = 0;
int hi = j;
int sumsCount = 0;
while (lo <= hi) {
int sum = values[lo] + values[hi];
if (sum > K) --hi;
else if (sum < K) ++lo;
else {
int coef = lo == hi ? 1 : 2;
sumsCount += coef * counts[lo] * counts[hi];
--hi;
++lo;
}
}
return sumsCount;
}
public static void main(String[] args) {
int[] a1 = new int[] {8, -2, 3, 5, 1, 1, 7, 4, 6};
int[] a2 = new int[] {1, 1, 1, 1};
int[] a3 = new int[] {1, 1, 1, 1, 2, 2, 2, 2};
int[] a4 = new int[] {1, 1, 1, 1, 2, 2, 2, 2, -1, 4};
int[] a5 = new int[] {-8, 2, -3, -5, -1, -1, -7, -4, -6};
assert (solution(6, a1) == 7);
assert (solution(2, a2) == 16);
assert (solution(1, a3) == 0);
assert (solution(2, a2) == 16);
assert (solution(3, a3) == 32);
assert (solution(3, a4) == 34);
assert (solution(-6, a5) == 7);
}
}