Sir, i am having dought in find() function

#include
#include
using namespace std;
int main()
{
int arr[]={20,30,40,40,40,40,50,100,1100};
int n=sizeof(arr)/sizeof(int);
int key;
cin>>key;
bool present=binary_search(arr,arr+n,key);
if(present)
{
cout<<“present”<<endl;
}
else
{
cout<<“absent”<<endl;
}
auto lb=lower_bound(arr,arr+n,key);
if((lb-arr)==n)
{
cout<<“not present”<<endl;

}
else
{
	cout<<(lb-arr)<<endl;
}
return 0;

}

sir this code working properly but when i am invin key as 12 it is showing 0 and if i am giving 444444 then is is giving me element is not present…
i want to say that this code is giving different answer for small number like 12,13 and giving right answer for big numbers like 122222or 4444, why so sir.

hello @Abhishmu5

if key is not present in array then lower bound return an iterator whose value is greater than key.
for ex-> key -> 4 , array-> 2 , 3, 9 , 10 ,12
here lower bound of 4 return iterator for 9 and clearly itr for 9 -array will not be n. so ur logic will fail.

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.