I'm only allowed to use the given parameters. I can't wrap my head around the thought of finding the index of the target. Any ideas?
#include <stdio.h>
int RecBinarySearch(int arr[], int len, int target) {
if (len <= 0)
return 0;
int mid = len/2;
if (target == arr[mid]){
return 1;
}
if (target < arr[mid]){
int i=0;
return RecBinarySearch(arr, mid, target);
}
else {
return RecBinarySearch(arr+mid+1, len-mid-1, target);
}
}
int main(void){
int arr[6]={1,2,3,4,5,6};
int len=sizeof(arr)/sizeof(arr[0]);
int target = 5;
RecBinarySearch(arr,len,target);
}
midwhen the target is found? And return e.g.-1when not found?