-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path43_firstAndLastOccurenceUsingRecursion.cpp
74 lines (63 loc) · 2.07 KB
/
43_firstAndLastOccurenceUsingRecursion.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
#include <iostream>
using namespace std;
int firstOccurence(int arr[],int start, int end, int key){
int mid = start + (end-start)/2;
int ans = -1;
// base condition
if(start > end)
return -1;
// recursive calls for finding the first occurence
if(arr[mid] == key){
ans = mid;
int mayAns = firstOccurence(arr,start,mid-1,key);
if(mayAns == -1){
return ans;
}
return mayAns;
}
else if(key > arr[mid]){
return firstOccurence(arr,mid+1,end,key);
}
else if(key < arr[mid]){
return firstOccurence(arr,start,mid-1,key);
}
}
int lastOccurence(int arr[],int start, int end, int key){
int mid = start + (end-start)/2;
int ans = -1;
// base case ?
if(start > end)
return -1;
// conditions for finding the last occurence
if(arr[mid] == key){
ans = mid;
int mayAns = lastOccurence(arr,mid+1,end,key);
if(mayAns == -1){
return ans;
}
return mayAns;
}
else if(arr[mid] < key){
return lastOccurence(arr,mid+1,end,key);
}
else if(arr[mid] > key){
return lastOccurence(arr,start,mid-1,key);
}
}
int main(){
// Here in this, we are going to find the first and last occurence of given key from the array using recursion. So, we are just going to see for the base condition and then using the recursive relation and recursive calling, the work will take place.
int arr[] = {1,2,3,3,3,3,4,5};
int size = 8;
cout<<"Array is: [1 2 3 3 3 3 4 5]"<<endl;
int key;
cout<<"Enter the key to find corresponding occurences: ";
cin>>key;
int start = 0;
int end = size-1;
int first = firstOccurence(arr,start,end,key);
int last = lastOccurence(arr,start,end,key);
cout<<"First Occurence of the given key is: "<<first<<endl;
cout<<"Last Occurence of the given key is: "<<last<<endl;
cout<<"Total Occurence of the given key is: "<<last-first+1<<endl;
return 0;
}