-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathTripleSum.java
More file actions
138 lines (98 loc) · 3.34 KB
/
Copy pathTripleSum.java
File metadata and controls
138 lines (98 loc) · 3.34 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package hackerrank;
/**
*
* @author Jayit
*/
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class TripleSum {
// this is the main func to process the triplet count
static long triplets(int[] a, int[] b, int[] c){
// initualize the counter
long distinctTripletCount = 0;
// remove duplicates from the arrays using set and HashMap Method
int[] distinctA = removeDuplicate(a);
int[] distinctB = removeDuplicate(b);
int[] distinctC = removeDuplicate(c);
// then sort the array so that it's easy to compare b array with a and c, as we need the numbers lesser than the values of b
Arrays.sort(distinctA);
Arrays.sort(distinctB);
Arrays.sort(distinctC);
// now the main part is to use forEach loop,
//for each of the items in array B, we need to use it as a key
//and then apply binary search on the arrays A and C to
//see where the binary search stops and returns
//add 1 to the value,
for (int q : distinctB) {
long c1 = getValidIndex(distinctA, q) + 1;
long c3 = getValidIndex(distinctC, q) + 1;
// total + no. of possible from a*no. of possible from c
distinctTripletCount += c1 * c3;
}
return distinctTripletCount;
}
private static int[] removeDuplicate(int[] a){
// Use a Set to just feed in the array
// A set has unique elements
// Then output the set into an array and return it
Set<Integer> set = new HashSet<>();
for(int item:a){
set.add(item);
}
int len = set.size();
int results[] = new int[len];
int i=0;
for(int item:set){
results[i++] = item;
}
return results;
}
static int getValidIndex(int[] distinctA, int key){
// arg1 -> array A or C , key->iterate over every value of b
// Implement Binary Search
int low = 0;
int high = distinctA.length - 1;
int count = -1;
while(low<=high){
int mid = (low+high)/2;
// This means that we need to increase low as the key is
// probably on the rt of low
if(distinctA[mid]<=key){
count = mid;
low = mid+1;
}
else{
high = mid -1;
}
}
return count;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int lena = sc.nextInt();
int lenb = sc.nextInt();
int lenc = sc.nextInt();
int a[] = new int[lena];
int b[] = new int[lenb];
int c[] = new int[lenc];
for (int i = 0; i < lena; i++) {
a[i] = sc.nextInt();
}
for (int i = 0; i < lenb; i++) {
b[i] = sc.nextInt();
}
for (int i = 0; i < lenc; i++) {
c[i] = sc.nextInt();
}
long result = triplets(a, b, c);
System.out.println(result);
sc.close();
}
}