Binary search test case 1 and 3

#include
#include

using namespace std;

int main()
{
int key,u,l,m,n,a[10000];
cin >> n;
for(int i=0;i<n;i++)
cin >> a[i];
cin >> key;
sort(a,a+n);
l=0;
u=n-1;
while(l<=u)
{
m=(u+l)/2;
if(key==a[m])
{
cout << m << endl;
break;
}
else if(key > a[m])
l=m+1;
else if(key < a[m])
u=m-1;
}
return 0;
}

Hi, your code doesnt consider the case when the number is not found in the array -1 has to be printed

also define an array of size n and not a[10000] it might not cover all cases

also since the i/p array is always in increasing order , u are not required to call the sort function

try doing all the changes by ur own
in case u fail
refer to the below code

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.