Binary Search Java Solution


  • 0
    C

    /You can minimize this code if you search mid element with high. But if you are comparing Mid with Low, foll is the code for it./

    public int findMin(int[] nums) {
    return helper(nums,0,nums.length-1);
    }

    public int helper(int[] nums, int low, int high){
    if(low==high){
    return nums[low];
    }
    int mid=(low+high)/2;
    int result;
    if(nums[mid]>nums[low]){
    int value2=helper(nums,mid+1,high);
    result=nums[low]<value2?nums[low]:value2;
    }
    else{
    int value2=helper(nums,low,mid);
    result=nums[mid+1]<value2?nums[mid+1]:value2;
    }
    return result;
    }


Log in to reply
 

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