1
1
#include < iostream>
2
- #include < vector>
3
- using namespace std ;
4
2
5
- bool linearSearchRecursive (int arr[], int n, int index, int target){
6
- if (index >= n){
7
- return false ;
8
- }
3
+ using namespace std ;
9
4
10
- if (arr[index] == target){
11
- return true ;
12
- }
5
+ int linearSearchRecursive (int arr[], int n, int index, int target) {
6
+ if (index >= n) {
7
+ return -1 ;
8
+ }
9
+
10
+ if (arr[index] == target) {
11
+ return index;
12
+ }
13
13
14
- return linearSearchRecursive (arr,n, index+ 1 , target);
14
+ return linearSearchRecursive (arr, n, index + 1 , target);
15
15
}
16
16
17
- int main (){
18
- int n;
19
- cout<< " Enter The size of array : " ;
20
- cin>> n;
21
- int arr[n];
22
- cout<< " Enter the element of array : " << endl;
23
- for (int i = 0 ;i<n; i++){
24
- cin>> arr[i];
25
- }
17
+ int main () {
18
+ int n;
19
+ cout << " Enter The size of array : " ;
20
+ cin >> n;
21
+ int arr[n];
22
+ cout << " Enter the element of array : " << endl;
23
+ for (int i = 0 ; i < n; i++) {
24
+ cin >> arr[i];
25
+ }
26
26
27
- int target;
28
- cout<< " Enter the element you need to search in the array : " ;
29
- cin>> target;
30
- bool answer = linearSearchRecursive (arr, n, 0 , target);
31
- if (answer) {
32
- cout<< " Found" << endl;
33
- } else {
34
- cout<< " Not Found" << endl;
35
- }
36
- }
27
+ int target;
28
+ cout << " Enter the element you need to search in the array : " ;
29
+ cin >> target;
30
+ int answer = linearSearchRecursive (arr, n, 0 , target);
31
+ if (answer >= 0 ) {
32
+ cout << " Target Element Found on index : " << answer << endl;
33
+ } else {
34
+ cout << " Target Element Not Found" << endl;
35
+ }
36
+ }
0 commit comments