I passed the test case 1 but the second test case was showing run error …Please find the solution. Mine code is attached below
#include<bits/stdc++.h>
using namespace std;
int search(int arr[],int s,int e,int key)
{
if(s>e)
{
return -1;
}
int mid=(s+e)/2;
if(key==arr[mid])
{
return mid;
}
if(arr[s]<=arr[mid])
{
if(key>=arr[s]&&key<=arr[mid])
{
return search(arr,s,mid-1,key);
}else
return search(arr,mid+1,e,key);
}
if(key>=arr[mid]&&key<=arr[e])
{
return search(arr,mid+1,e,key);
}else
return search(arr,s,mid-1,key);
}
int main()
{
int arr[100];
int n;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>arr[i];
}
int key;
cin>>key;
int s=0;
int e=n-1;
cout<<search(arr,s,e,key);
return 0;
}