Skip to Main Content

Binary Search

Problem URL:Binary Search

My Solution

Python

def search(nums: List[int], target: int) -> int:
    index = 0
    nums_copy = nums[:]
    midpoint = len(nums_copy)//2

    while len(nums_copy) > 1 and target != nums_copy[midpoint]:
        if target > nums_copy[midpoint]:
            index += midpoint
            nums_copy[:] = nums_copy[midpoint:]
        else:
            nums_copy = nums_copy[:midpoint]

        midpoint = len(nums_copy)//2


    return index + midpoint if target == nums_copy[midpoint] else -1

JavaScript / TypeScript

const search = (nums: number[], target: number): number => {
  let left = 0,
    right = nums.length - 1,
    midpoint = -1;

  while (left <= right) {
    midpoint = Math.ceil((left + right) / 2);

    if (target < nums[midpoint]) {
      right = midpoint - 1;
    } else if (target > nums[midpoint]) {
      left = midpoint + 1;
    } else {
      return midpoint;
    }
  }

  return -1;
};

Let's Connect

Twitter GitHub LinkedIn