-
Notifications
You must be signed in to change notification settings - Fork 0
/
13_Ques_BinarySearch.cpp
60 lines (53 loc) · 1.4 KB
/
13_Ques_BinarySearch.cpp
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
// code studio Question:
// Note: Array should be sorted.
// find First and last occurance of the element in the array.
// {1,2,2,3,4,2,5} -> find occurance of the two
// first occurance of 2 is 1 and last occurance of 2 is 6
#include <iostream>
using namespace std;
int firstOccr(int arr[], int size, int key) {
int s = 0;
int e = size - 1;
int midle = s + (e - s) / 2;
int ans = -1;
while (s <= e) {
if (arr[midle] == key) {
ans = midle;
e = midle - 1;
} else if (key > arr[midle]) {
s = midle + 1;
} else if (key < arr[midle]) {
e = midle - 1;
}
midle = s + (e - s) / 2;
}
return ans;
}
int lastOccr(int arr[], int size, int key) {
int s = 0;
int e = size - 1;
int mid = s + (e - s) / 2;
int ans = -1;
while (s <= e) {
if (arr[mid] == key) {
ans = mid;
s = mid + 1;
} else if (key > arr[mid]) {
s = mid + 1;
} else if (key < arr[mid]) {
e = mid - 1;
}
mid = s + (e - s) / 2;
}
return ans;
}
int main() {
int oddarr[7] = {1, 2, 4, 4, 4, 5, 6};
int evenarr[6] = {2, 5, 3, 5, 3, 2};
int test[15]={1,2,2,2,3,3,3,3,3,3,3,3,3,3,3};
cout << "first occurance at " << firstOccr(oddarr, 7, 3) << endl;
cout << "last occurance at " << lastOccr(oddarr, 7, 3)<<endl;
cout << "first occurance at " << firstOccr(test, 15, 3) << endl;
cout << "last occurance at " << lastOccr(test, 15, 3);
return 0;
}