-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountInSortedArrayOfDuplicates.cpp
More file actions
69 lines (61 loc) · 1.88 KB
/
CountInSortedArrayOfDuplicates.cpp
File metadata and controls
69 lines (61 loc) · 1.88 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
#include <iostream>
#include <vector>
// QUESTION : https://www.techiedelight.com/count-occurrences-number-sorted-array-duplicates/
// Count occurences of a number in a sorted array with duplicates
class Counter
{
private:
const std::vector<int>& arr;
public:
Counter(const std::vector<int>& a)
: arr(a)
{
}
int find(int number)
{
return helper(0, arr.size(), number);
}
private:
int helper(int low, int high, int number)
{
if (low >= 0 && high < arr.size() && arr[low] == arr[high] && arr[low] == number)
{
return high - low + 1;
}
if (low < high)
{
int mid = low + (high - low) / 2;
if (arr[mid] == number) {
// This could be the first, last or mid element -> find its position!
if ((mid + 1 < arr.size()) && (mid - 1 > 0) && (arr[mid - 1] == number) && (arr[mid + 1] == number)) {
// mid position -> look for more in left and right directions
return 1 + helper(low, mid - 1, number) + helper(mid + 1, high, number);
} else if (mid + 1 < arr.size() && arr[mid + 1] == number) {
// first position (nothing on the left)
return 1 + helper(mid + 1, high, number);
} else if (mid - 1 > 0 && arr[mid - 1] == number) {
// last position (nothing on the right)
return 1 + helper(low, mid - 1, number);
} else {
// element occurs only once
return 1;
}
} else if (arr[mid] > number) {
return helper(low, mid, number);
} else {
return helper(mid + 1, high, number);
}
}
}
};
int main()
{
std::vector<int> arr{ 2, 5, 5, 5, 6, 6, 8, 9, 9, 9 };
Counter obj(arr);
std::cout << "2 occurs " << obj.find(2) << " times" << std::endl;
std::cout << "5 occurs " << obj.find(5) << " times" << std::endl;
std::cout << "6 occurs " << obj.find(6) << " times" << std::endl;
std::cout << "8 occurs " << obj.find(8) << " times" << std::endl;
std::cout << "9 occurs " << obj.find(9) << " times" << std::endl;
return 0;
}