-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTripleSum.java
87 lines (73 loc) · 2.66 KB
/
TripleSum.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// https://www.hackerrank.com/challenges/triple-sum/problem
package search;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class TripleSum {
private static final Scanner SCANNER = new Scanner(System.in);
public static void main(String[] args) {
int a = SCANNER.nextInt();
int b = SCANNER.nextInt();
int c = SCANNER.nextInt();
int[] first = getUniqueArray(a);
int[] second = getUniqueArray(b);
int[] third = getUniqueArray(c);
System.out.println(possibleTriplets(first, second, third));
}
private static long possibleTriplets(int[] first, int[] second, int[] third) {
sortAll(first, second, third);
int[] bGreaterThanC = greaterThan(second, third);
long[] sum = sumFromRight(bGreaterThanC);
long count = 0;
for (int index = first.length - 1, pointer = second.length - 1 ; index >= 0 ; index--) {
while (pointer != -1 && first[index] <= second[pointer]) {
pointer--;
}
count += pointer == second.length - 1 ? 0 : sum[pointer + 1];
}
return count;
}
private static int[] greaterThan(int[] first, int[] second) {
int[] result = new int[first.length];
for (int index = first.length - 1, pointer = second.length - 1 ; index >= 0 && pointer >= 0 ; index-- ) {
if (first[index] >= second[pointer]) {
result[index] = pointer + 1;
continue;
}
while (pointer >= 0 && first[index] < second[pointer]) {
pointer--;
}
result[index] = pointer + 1;
}
return result;
}
private static long[] sumFromRight(int[] array) {
long[] result = new long[array.length];
result[result.length - 1] = array[array.length - 1];
for (int index = array.length - 2 ; index >= 0 ; index--) {
result[index] = result[index + 1] + array[index];
}
return result;
}
private static void sortAll(int[] first, int[] second, int[] third) {
Arrays.sort(first);
Arrays.sort(second);
Arrays.sort(third);
}
private static int[] getUniqueArray(int length) {
Set<Integer> set = new HashSet<>();
for (int index = 0 ; index < length ; index++) {
set.add(SCANNER.nextInt());
}
return toArray(set);
}
private static int[] toArray(Set<Integer> set) {
int[] array = new int[set.size()];
int index = 0;
for (int number : set) {
array[index++] = number;
}
return array;
}
}