Binary search Standard Template Library

What if 40 is present at the last index of array like…
arr[] = {20,30,40,40,50,100,40}

Then how to find the last index of 40 because upper_bound function will give answer as 3.

Binary search works only when array is sorted

1 Like

Adding to what @sdevwrat wrote -
Implementation of both upper_bound() and lower_bound() are done using binary_search() so the array needs to be sorted.

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.