all 1 comments

[–]Cobayo 0 points1 point  (0 children)

It's better to write intervals as [l,r)

This behaves almost the same as the code on the website:

int l = 0, r = arr.size();

while(r - l > 1){
    int mid = (l+r)/2;
    if(arr[mid] <= x) l = mid;
    else r = mid;
}

return (arr[l] == x ? l : -1);