1 of 4
Write a function binary_search(nums, target) that searches for target in a sorted list nums.
Return the index of target if found, or -1 if it is not in the list.
print(binary_search([1, 3, 5, 7, 9], 5)) # 2 print(binary_search([1, 3, 5, 7, 9], 7)) # 3 print(binary_search([1, 3, 5, 7, 9], 4)) # -1 print(binary_search([], 1)) # -1
| Complexity | |
|---|---|
| Time | O(log n) — halve the search space each step |
| Space | O(1) — only two pointers |
Keep two pointers, lo and hi, that mark the current search window:
lo = 0, hi = len(nums) - 1lo <= hi:
mid = (lo + hi) // 2nums[mid] == target → found, return midnums[mid] < target → target is in the right half, set lo = mid + 1nums[mid] > target → target is in the left half, set hi = mid - 1-1def binary_search(nums, target): lo = 0 hi = len(nums) - 1 while lo <= hi: mid = (lo + hi) // 2 # Your code here pass return -1 print(binary_search([1, 3, 5, 7, 9], 5)) # 2 print(binary_search([1, 3, 5, 7, 9], 7)) # 3 print(binary_search([1, 3, 5, 7, 9], 4)) # -1 print(binary_search([], 1)) # -1
Check three cases at mid:
if nums[mid] == target: return mid elif nums[mid] < target: lo = mid + 1 # target must be to the right else: hi = mid - 1 # target must be to the left
The loop condition lo <= hi keeps going as long as the search window has at least one element. When lo > hi the window is empty and the target cannot exist — return -1.
def binary_search(nums, target): lo = 0 hi = len(nums) - 1 while lo <= hi: mid = (lo + hi) // 2 if nums[mid] == target: return mid elif nums[mid] < target: lo = mid + 1 else: hi = mid - 1 return -1
Why this works: Each iteration cuts the remaining search space in half. After at most log₂(n) iterations, either the target is found or the window collapses to nothing.
Modify your function to handle a list that is sorted in descending order (largest first).
print(binary_search_desc([9, 7, 5, 3, 1], 5)) # 2 print(binary_search_desc([9, 7, 5, 3, 1], 4)) # -1