No need for search low and hi in two separate binary search, we can do it in one pass!

  • 0

    It seems that most of the high voted answers use two passes of binary search, which is not necessary, the nature of the binary search already gives us the bound we need

    public class Solution {
    public int[] searchRange(int[] nums, int target) {
        int lo = 0, hi = nums.length-1;
        while(lo <= hi) {
            int mid = (lo+hi)/2;
            if(nums[mid] == target) {
                if(nums[hi] > target)
                else if(nums[lo] < target)
                    return new int[] {lo, hi};
            else if(nums[mid] < target) {
                lo = mid + 1;
            else {
                hi = mid - 1;
        return new int[] {-1, -1};


  • 0

    The time complexity of your algorithm will reach O(n) rather than O(lg(n)) in the case that upper_bound - low_bound is O(n). Not a good solution in all cases.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.