If you are doing this on a sorted array arr then would bisect_left(arr, x) == bisect_right(arr, x-1) under any circumstances? Continue reading...