Repository navigation
Expand file tree
/
Copy pathLecture13.cpp
More file actions
86 lines (71 loc) · 2.27 KB
/
Copy pathLecture13.cpp
File metadata and controls
86 lines (71 loc) · 2.27 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// #include<iostream>
// #include<vector>
// using namespace std;
// peakIndexMountainArray
// int peakIndexMountainArray(int arr[], int n) {
// int s = 0;
// int e = n-1;
// int mid = s + (e-s)/2;
// while(s<e) {
// if(arr[mid] < arr[mid+1])
// {
// s = mid+1; // here is not any problem with start that's why we take s = mid+1 .
// }
// else{ // in 2nd slope (e) and mid could be at peak of the mountain .
// e = mid; // here is a problem with end (e) that's why we take e = mid .
// }
// mid = s + (e-s)/2;
// }
// return s;
// }
// TOTAL NUMBER OF OCCURENCES
// find first and last occurence and then
// total no. of occurence = (last-first)occurence index + 1
// FIRST AND LAST OCCURENCE
// int firstOcc(int arr[], int n, int key) {
// int s = 0, e = n-1;
// int mid = s + (e-s)/2;
// int ans1 = -1;
// while(s<=e) {
// if(arr[mid] == key){
// ans1 = mid;
// e = mid - 1;
// }
// else if(key > arr[mid]) {//Right me jao
// s = mid + 1;
// }
// else if(key < arr[mid]) {//left me jao
// e = mid - 1;
// }
// mid = s + (e-s)/2;
// }
// return ans1;
// }
// int lastOcc(int arr[], int n, int key) {
// int s = 0, e = n-1;
// int mid = s + (e-s)/2;
// int ans2 = -1;
// while(s<=e) {
// if(arr[mid] == key){
// ans2 = mid;
// s = mid + 1;
// }
// else if(key > arr[mid]) {//Right me jao
// s = mid + 1;
// }
// else{//left me jao
// e = mid - 1;
// }
// mid = s + (e-s)/2;
// }
// return ans2;
// }
// int main(){
// int arr[5] = {2,4,8,8,12};
// cout<<"first occ at index "<<firstOcc(arr, 5, 8)<<endl;;
// cout<<"last occ at index "<<lastOcc(arr, 5, 8)<<endl;
// int totalocc;
// totalocc = (lastOcc(arr,5,8) - firstOcc(arr,5,8)) + 1;
// cout<<totalocc<<endl;
// return 0;
// }