What is wrong in this code

#include
using namespace std;
int fun(int arr[100],int n,int key)
{
int s=0;
int e=n-1;
while(s<=e)
{
int mid=(s+e)/2;
if(arr[mid]==key)
{
return mid;
}
else if(arr[s]<=arr[mid])
{
if(key>=arr[s] && key<=arr[mid])
{
e=mid-1;
}
else{
s=mid+1;
}
}
else {
if(key>=arr[mid] && key<=arr[e])
{
s=mid+1;
}
else{
e=mid-1;
}
}
}
return -1;
}
int main() {
int n;
cin>>n;
int arr[100];
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
int key;
cin>>key;
cout<<fun(arr,n,key);
return 0;
}

hello @Kunalgoyal
your code is correct. the issue is with the array size ,100 is not enough.

to make it work

declare ur array as
image

and change declaration as image

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.