Help rahul to search

hello i have solved the problem by my own but not satisfied with it…for better approach i opened solution but i didnt understand the logic and it was also in java…pls explain the the solution provided with the problem beforehand

my code

#include
using namespace std;
#include
long long int bs(long long int *arr,long long int s,long long int n,long long int x){
long long int start=s;
long long int end=n;
long long int ans=0;
while(start<=end){
long long int mid=start+((end-start)/2);
if(arr[mid]==x){
return mid;
}
else if(arr[mid]<x){
start=mid+1;
}
else{
end=mid-1;
}
}
return -1;
}

int main() {
long long int n;
cin>>n;
long long int arr[10000];
for(long long int i=0;i<n;i++){
cin>>arr[i];
}
long long int count =0;
bool inc=false;
for(long long int i=0;i<n-1;i++){
if(arr[i+1]<arr[i]){
inc =true;
break;
}
count++;
}
//cout<<count<<" ";
long long int x;
cin>>x;
long long int ans1=bs(arr,0,count,x);
if(ans1==-1){
ans1=bs(arr,count+1,n-1,x);
}
cout<<ans1;
}

given solution

public static int search(int arr[], int l, int h, int key) {

if (l > h)  
    return -1; 

int mid = (l+h)/2; 
if (arr[mid] == key) 
    return mid; 

// If arr[l...mid] is sorted 
if (arr[l] <= arr[mid]) { 

    if (key >= arr[l] && key <= arr[mid]) 
        return search(arr, l, mid-1, key); 

    return search(arr, mid+1, h, key); 
} 

if (key >= arr[mid] && key <= arr[h]) 
    return search(arr, mid+1, h, key); 

return search(arr, l, mid-1, key); 

}

my code in ide

hi @rabimajumder08
refer this -->

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.