-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLesson15(CaterpillarMethod)-AbsDistinct.cpp
More file actions
58 lines (51 loc) · 1.78 KB
/
Copy pathLesson15(CaterpillarMethod)-AbsDistinct.cpp
File metadata and controls
58 lines (51 loc) · 1.78 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
// 1. AbsDistinct
/**
* A non-empty array A consisting of N numbers is given.
* The array is sorted in non-decreasing order.
* The absolute distinct count of this array
* is the number of distinct absolute values among the elements of the array.
*
* For example, consider array A such that:
* A[0] = -5 A[1] = -3 A[2] = -1 A[3] = 0 A[4] = 3 A[5] = 6
* The absolute distinct count of this array is 5,
* because there are 5 distinct absolute values among the elements of this array, namely 0, 1, 3, 5 and 6.
*
* Write a function:
* class Solution { public int solution(int[] A); }
* that, given a non-empty array A consisting of N numbers, returns absolute distinct count of array A.
*
* Write an efficient algorithm for the following assumptions:
* • N is an integer within the range [1..100,000];
* • each element of array A is an integer within the range [−2,147,483,648..2,147,483,647];
* • array A is sorted in non-decreasing order.
*/
#include <vector>
#include <algorithm>
int absDistinct(std::vector<int>& A)
{
int absDistinctCount = 0,
head = 0,
tail = (int)A.size() - 1;
while (head <= tail) {
// Move the head and tail pointers towards each other.
while (head < tail && A[head] == A[head + 1]) {
head++;
}
while (head < tail && A[tail] == A[tail - 1]) {
tail--;
}
// Check for distinct absolute values.
long absHead = std::abs((long)A[head]),
absTail = std::abs((long)A[tail]);
if (absHead == absTail) {
head++;
tail--;
} else if (absHead < absTail) {
tail--;
} else {
head++;
}
absDistinctCount++;
}
return absDistinctCount;
}