-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy path02_last_occurence.cpp
More file actions
112 lines (97 loc) · 2.24 KB
/
02_last_occurence.cpp
File metadata and controls
112 lines (97 loc) · 2.24 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
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
/*
Topic - Last Occurence / Linear Search
*/
#include <iostream>
using namespace std;
// find last occurence in linear search (recursion)
int lastOcc(int *arr, int size, int key)
{
// base case
if (size == 0)
{
return -1;
}
// rec case
int idx = lastOcc(arr + 1, size - 1, key); // recursive call
if (idx == -1) // check for search value
{
if (arr[0] == key)
{
// return the idx, when search value is found
return 0;
}
else
{
// if search value is not found till now
return -1;
}
}
// if idx returned by subproblem is not -1, then increment the idx to update it according to current
// array position
return idx + 1;
/*
APPROACH:
- first make a call on remaining part
- then check KEY with current index
*/
}
// find first occurence in linear search (recursion)
int firstOcc(int arr[], int size, int key)
{
// base case
if (size == 0)
{
return -1;
}
// recursive case
if (arr[0] == key) // check for search value
{
return 0;
}
int idx = firstOcc(arr + 1, size - 1, key); // recursive call
if (idx == -1)
{
return -1;
}
return idx + 1;
/*
APPROACH:
- first check KEY with current index
- then make a call on remaining part
*/
}
// function to drive code
int main()
{
int size;
cout << "Enter array size: ";
cin >> size;
int arr[size];
cout << "Enter Elements: ";
for (int i = 0; i < size; i++)
{
cin >> arr[i];
}
int key;
cout << "Enter Search Key: ";
cin >> key;
// first & last occurence in linear search
cout << "first occurence : " << firstOcc(arr, size, key) << endl;
cout << "last occurence : " << lastOcc(arr, size, key) << endl;
return 0;
}
/*
OUTPUT:
Case 1:
Enter array size: 6
Enter Elements: 1 2 7 3 7 6
Enter Search Key: 7
first occurence : 2
last occurence : 4
Case 2:
Enter array size: 6
Enter Elements: 1 2 17 3 17 6
Enter Search Key: 7
first occurence : -1
last occurence : -1
*/