-
Notifications
You must be signed in to change notification settings - Fork 0
/
absDistinct.js
54 lines (47 loc) · 1.98 KB
/
absDistinct.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
// -----------------------------------------------------------------------------
// ABS DISTINCT
// Compute number of distinct absolute values of sorted array elements.
//
// Report: https://app.codility.com/demo/results/training33PKZ5-JJX/
// -----------------------------------------------------------------------------
function solution(A) {
// seems naive to me, but gets 100% score
// I tried some other approaches without sorting but got less than perfect score
A = A.map((a) => Math.abs(a))
A.sort((a,b) => a - b)
let back = 0
let front = back + 1
let dup = 0
while (back < A.length) {
while (front < A.length && A[back] == A[front]) {
front +=1
}
dup += front - back - 1;
back = front
front += 1
}
return A.length - dup;
}
console.log(solution([0,0,1,1,1,1,3,3])) // 3
console.log(solution([-3,-2,-2,-1,0,1,1,1,1,2,3,3])) // 4
console.log(solution([0,0,0,1,2,3,3,3])) // 4
console.log(solution([0,0,0,0])) // 1
console.log(solution([0,0,0,0,0])) // 1
console.log(solution([0,1,3,3,5])) // 4
console.log(solution([0,1,3,4,6])) // 5
console.log(solution([-5,1,3,3,3,3,3,3,3,3,5])) // 3
console.log(solution([-1, -3, -3, -2, -2])) // 3
console.log(solution([-5, -3, -1, 0, 3, 6])) // 5
console.log(solution([-5, -3, -1, 0, 3, 3, 3, 3, 6])) // 5
console.log(solution([-5, -3, -3, -1, 0, 3, 6])) // 5
console.log(solution([-5, -3, -1, 0, 3, 3, 6])) // 5
console.log(solution([-5, -3, -1, 0, 2, 4, 6])) // 7
console.log(solution([-5, 0, 0, 0, 0])) // 2
console.log(solution([
-2147483648, -2147483648, -2147483648, -2147483648, -2147483648,
-2147483648, -2147483648, -2147483648, -2147483648, -2147483648,
-2147483648, -2147483648, -2147483648, -2147483648, -2147483648,
2147483647, 2147483647, 2147483647, 2147483647, 2147483647,
2147483647, 2147483647, 2147483647, 2147483647, 2147483647,
2147483647, 2147483647, 2147483647, 2147483647, 2147483647,
2147483647, 2147483647])) // 2