forked from Haresh1204/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
First and last binary search .cpp
66 lines (62 loc) · 1.54 KB
/
First and last binary search .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
61
62
63
64
65
66
#include <iostream>
using namespace std;
int firstOcurrence(int arr[], int n, int key)
{
int s = 0;
int e = n - 1;
int mid = s + (e - s) / 2;
int ans = -1;
while (s <= e)
{
if (arr[mid] == key)
{
ans = mid;
e = mid - 1;
}
else if (key > arr[mid]) // go to right part
{
s = mid + 1;
}
else if (key < arr[mid]) // go to left part
{
e = mid - 1;
}
mid = s + (e - s) / 2;
}
}
int lastOcurrence(int arr[], int n, int key)
{
int s = 0;
int e = n - 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]) // Right wale part mei jaana h
{
s = mid + 1;
}
else if (key < arr[mid]) // Left wale part mei jaana h
{
e = mid - 1;
}
mid = s + (e - s) / 2;
}
}
int main()
{
int arr[4] = {3, 4, 5, 1};
int ans = PeakElement(arr, 4);
cout << ans << endl;
// int arr[8] = {1, 2, 3, 3, 3, 3, 3, 5};
// cout << "First Occurrence of 3 is at index : " << firstOcurrence(arr, 8, 3) << endl;
// cout << "Last Occurence of 3 is at index : " << lastOcurrence(arr, 8, 3) << endl;
// int totalCount = (lastOcurrence(arr, 8, 3) - firstOcurrence(arr, 8, 3));
// cout << totalCount << endl;
return 0;
}