forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCount Special Quadruplets.js
59 lines (48 loc) · 1.53 KB
/
Count Special Quadruplets.js
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
/**
* @param {number[]} nums
* @return {number}
*/
var countQuadruplets = function(nums) {
const map = new Map();
for(let i = 0; i < nums.length; i++) {
let n = nums[i];
if(map.has(n)) {
let t = map.get(n);
t.push(i);
map.set(n, t);
} else {
map.set(n, [i]);
}
}
let count = 0;
for(let a = 0;a < nums.length - 3; a++) {
for(let b = a + 1; b < nums.length - 2; b++) {
for(let c = b + 1; c < nums.length - 1;c++) {
let sum = nums[a] + nums[b] + nums[c];
let indexes = map.get(sum) || [];
//t is the d index that might exist
let t = c + 1;
let low = 0;
let high = indexes.length - 1;
let ans = -1;
while(low <= high) {
let mid = low + Math.floor((high - low)/2);
if(indexes[mid] === t) {
ans = mid;
break;
}
if(indexes[mid] < t) {
low = mid + 1;
} else {
ans = mid;
high = mid - 1;
}
}
if(ans !== -1) {
count += indexes.length - ans;
}
}
}
}
return count;
};