Find Peak Element Using Binary Search in DSA Python - Time & Space Complexity
We want to know how fast the binary search method finds a peak element in an array.
How does the number of steps grow as the array gets bigger?
Analyze the time complexity of the following code snippet.
def find_peak(nums):
left, right = 0, len(nums) - 1
while left < right:
mid = (left + right) // 2
if nums[mid] < nums[mid + 1]:
left = mid + 1
else:
right = mid
return left
This code finds a peak element by repeatedly narrowing the search range using binary search.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that halves the search range each time.
- How many times: The loop runs until the search space is reduced to one element, about log base 2 of n times.
Each step cuts the array size roughly in half, so the number of steps grows slowly as the array grows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 4 steps |
| 100 | About 7 steps |
| 1000 | About 10 steps |
Pattern observation: Doubling the input size adds only one more step.
Time Complexity: O(log n)
This means the steps needed grow very slowly even if the array gets much bigger.
[X] Wrong: "The loop runs n times because it looks like it checks each element."
[OK] Correct: Actually, the loop cuts the search space in half each time, so it runs much fewer times than n.
Understanding this binary search approach shows you can find answers quickly by cutting down options step-by-step, a key skill in many coding problems.
"What if we changed the array to be unsorted without any peak guarantee? How would the time complexity change?"
Practice
Solution
Step 1: Understand the definition of a peak element
A peak element is one that is strictly greater than its neighbors on both sides (or just one side if at the boundary).Step 2: Relate to binary search goal
Binary search finds such an element by comparing middle elements with neighbors to decide search direction.Final Answer:
An element that is greater than its neighbors -> Option BQuick Check:
Peak element = greater than neighbors [OK]
- Thinking peak is smallest element
- Confusing peak with first or last element always
- Assuming peak equals neighbors
nums?Solution
Step 1: Identify binary search loop condition
Binary search typically useswhile left < right:to narrow down the search space until left meets right.Step 2: Compare options
while left <= right:is common in classic binary search but for peak finding,while left < right:is preferred to avoid infinite loops.Final Answer:
while left < right: -> Option DQuick Check:
Binary search loop uses < not <= [OK]
- Using for loop instead of while
- Using <= which may cause infinite loop
- Using if instead of loop
nums = [1, 3, 20, 4, 1, 0] using binary search peak finding?Solution
Step 1: Identify peak candidates
Check elements: 20 at index 2 is greater than neighbors 3 and 4, so index 2 is a peak.Step 2: Binary search logic
Binary search will find index 2 by comparing mid elements and moving left or right accordingly.Final Answer:
2 -> Option AQuick Check:
Peak at index 2 with value 20 [OK]
- Choosing first or last element incorrectly
- Confusing index with value
- Not checking both neighbors
def find_peak(nums):
left, right = 0, len(nums) - 1
while left <= right:
mid = (left + right) // 2
if nums[mid] < nums[mid + 1]:
left = mid + 1
else:
right = mid - 1
return leftSolution
Step 1: Check index usage in condition
The code accessesnums[mid + 1]without checking ifmid + 1is within array bounds.Step 2: Identify potential error
Ifmidis at the last index,mid + 1causes an out of range error.Final Answer:
Index mid + 1 may cause out of range error -> Option AQuick Check:
Accessing nums[mid + 1] risks out of range [OK]
- Ignoring array boundary checks
- Using <= in loop causing infinite loop
- Returning wrong index
nums = [10, 20, 15, 2, 23, 90, 67]. Using binary search, which index will be returned as a peak element? Consider multiple peaks exist.Solution
Step 1: Identify all peak elements
Peaks are at index 1 (20 > 10 and 15), index 5 (90 > 23 and 67).Step 2: Understand binary search behavior
Binary search may return any peak, but typically it finds the one in the middle or right side depending on comparisons.Step 3: Trace binary search on given array
Mid starts near center; since nums[mid] < nums[mid+1] at some point, search moves right, finally returning index 5.Final Answer:
5 -> Option CQuick Check:
Binary search finds peak at index 5 with value 90 [OK]
- Assuming only first peak is returned
- Ignoring binary search direction logic
- Confusing index with value
Ready to prove it?
Pass the test and this pattern moves unknown
