Binary Search Recursion , It not return -1

#include

using namespace std;

int BinaryRecursion(int A[],int start,int end,int key)
{
if (end>=1)
{
int mid=(start+end)/2;

    if (A[mid]==key)
    {
        return mid;
    }
    if (A[mid]>key)
    {
        return BinaryRecursion(A,start,mid-1,key);
    }
    else
    {
        return BinaryRecursion(A,mid+1,end,key);
    }
    
}
return -1;

}

int main()
{
int num;
cin>>num;
int Array[num];

for (int i = 0; i < num; i++)
{
    cin>>Array[i];
}
int key;
cin>>key;
int start=0;

cout<<BinaryRecursion(Array,start,num-1,key);
return 0;

}

Hey @rprahulpal03
Please share your code in Coding blocks ide

This should be end>=start

thanks now its working

1 Like

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.