-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlec13_binaryques.cpp
121 lines (101 loc) · 2.21 KB
/
lec13_binaryques.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// First and last occurce of a key
/*
#include <iostream>
using namespace std;
int firstoccur(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;
e = mid - 1;
}
else if (arr[mid] > key)
{
e = mid - 1;
}
else if (arr[mid] < key)
{
s = mid + 1;
}
mid = s + (e - s) / 2;
}
return ans;
}
int lastoccur(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 (arr[mid] > key)
{
e = mid - 1;
}
else if (arr[mid] < key)
{
s = mid + 1;
}
mid = s + (e - s) / 2;
}
return ans;
}
int main()
{
int arz[6] = {0, 5, 5, 6, 6, 6};
int arx[8] = {0, 1, 1, 1, 2, 2, 2, 2};
int arc[1] = {2};
cout << "index of first occurence of 6 is " << firstoccur(arz, 6, 6) << endl;
cout << "index of last occurence of 6 is " << lastoccur(arz, 6, 6) << endl;
cout << endl;
cout << "index of first occurence of 2 is " << firstoccur(arx, 8, 2) << endl;
cout << "index of last occurence of 2 is " << lastoccur(arx, 8, 2) << endl;
cout << endl;
cout << "index of first occurence of 2 is " << firstoccur(arc, 1, 2) << endl;
cout << "index of last occurence of 2 is " << lastoccur(arc, 1, 2) << endl;
cout << endl;
}
*/
// Find total no. of occurence
/*
total no. of occurence = (lastoccurindex - firstoccurindex)+1
*/
// Peak index in mountain array
#include <iostream>
using namespace std;
int peakmoun(int arr[], int size)
{
int s = 0;
int e = size - 1;
int mid = s + (e - s) / 2;
while (s < e)
{
if (arr[mid] < arr[mid + 1])
{
s = mid + 1;
}
else
{
e = mid;
}
mid = s + (e - s) / 2;
}
return s;
}
int main()
{
int arr[7] = {3, 4, 5, 6, 7, 3, 1};
cout << peakmoun(arr, 7);
}