-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse string.cpp
More file actions
50 lines (43 loc) · 1.09 KB
/
Copy pathreverse string.cpp
File metadata and controls
50 lines (43 loc) · 1.09 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
#include <iostream>
using namespace std;
void lastOccurrence(int s, int array[], int key) {
int start = 0;
int end = s - 1;
int result = -1;
while (start <= end) {
int mid = (start + end) / 2;
if (array[mid] == key) {
result = mid;
start = mid +1;
} else if (key < array[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
cout << "First occurrence at: " << result << endl;
}
void firstOccurrence(int s, int array[], int key) {
int start = 0;
int end = s - 1;
int result = -1;
while (start <= end) {
int mid = (start + end) / 2;
if (array[mid] == key) {
result = mid;
end = mid -1;
} else if (key < array[mid]) {
end = mid - 1;
} else {
start = mid + 1;
}
}
cout << "First occurrence at: " << result << endl;
}
int main() {
int array[] = {1, 2, 2, 2, 3, 4, 5};
int size = sizeof(array) / sizeof(array[0]);
int key = 2;
firstOccurrence(size, array, key);
return 0;
}