Find Minimum in Rotated Sorted Array

This one looks scary at first. A sorted array got rotated, and now you have to find the smallest number. The interviewer wants to see if you can still use binary search even when the array is not in perfect order. That is the real test here. Spot the pattern and the answer drops out fast.

🎯 The Problem

You get a sorted array that was rotated, and you must find the smallest number. Here are the rules.

  • A rotation takes some numbers from the front and moves them to the back, keeping their order.
  • So sorted [0, 1, 2, 4, 5, 6, 7] might become [4, 5, 6, 7, 0, 1, 2].
  • The result is two sorted pieces. The left piece and the right piece are each sorted.
  • The smallest number sits right where the second piece begins.
  • Find that smallest number. Every number is different.

In [4, 5, 6, 7, 0, 1, 2] the smallest number is 0, so the answer is 0.

Input: nums = [4, 5, 6, 7, 0, 1, 2]
Output: 0
Explanation: the array was the sorted [0,1,2,4,5,6,7] rotated, and 0 is the smallest value

Here is the rotated array. See how it climbs, then drops, then climbs again. The drop point holds the smallest number.

idx 0: 4

idx 1: 5

idx 2: 6

idx 3: 7 highest

idx 4: 0 smallest

idx 5: 1

idx 6: 2

🐒 Approach 1: Linear Scan (Brute Force)

The idea in one line: look at every number and keep the smallest one you have seen.

The idea:

  • Assume the first number is the smallest.
  • Walk through the rest of the array.
  • Each time you find a smaller number, remember it.

How it works:

  • This is a plain linear scan. You visit every item, one after another.
  • It always gives the right answer.

Why it is weak:

  • The array is two sorted pieces, but the scan ignores that.
  • For a big array you check every single number.
  • That is O(n) time.

Here is the linear-scan code for that idea:

find_min_rotated_linear.py
def find_min(nums):
smallest = nums[0]
for num in nums:
smallest = min(smallest, num)
return smallest

⚑ Approach 2: Binary Search on the Drop (Best)

The idea in one line: the smallest number sits at the single drop point, so binary search for that drop.

The idea:

  • The array climbs, drops once, then climbs again.
  • That single drop point is exactly where the smallest number lives.
  • Keep two markers. lo is the left edge, hi is the right edge.

How it works:

  • Pick the middle position mid. Compare the middle number with the number at hi, the rightmost in the window.
  • If the middle is bigger than the number at hi, the drop is to the right. Move lo to mid + 1.
  • If the middle is smaller than or equal to the number at hi, the right half is already sorted. The smallest is at mid or to its left. Move hi to mid.

Why it is fast:

  • Move hi to mid, not mid - 1, because mid itself could be the smallest. Keep it in the window.
  • Stop when lo and hi meet. They land on the smallest number.
  • Each step halves the window, so this is O(log n) time.

Here is a dry run on [4, 5, 6, 7, 0, 1, 2]. Watch lo, mid and hi narrow toward the smallest value.

Step 1: lo=0 hi=6 mid=3 nums[mid]=7 ... 7 greater than nums[hi]=2 go right lo=4

Step 2: lo=4 hi=6 mid=5 nums[mid]=1 ... 1 less than nums[hi]=2 go left hi=5

Step 3: lo=4 hi=5 mid=4 nums[mid]=0 ... 0 less than nums[hi]=1 go left hi=4

Step 4: lo=4 hi=4 ... lo meets hi answer is nums[4]=0

Steps to Solve

  1. Set lo to 0 and hi to the last position of the array.
  2. While lo is less than hi, keep narrowing the window.
  3. Find the middle position mid as lo + (hi - lo) / 2.
  4. If the number at mid is bigger than the number at hi, move lo to mid + 1.
  5. Otherwise move hi to mid, keeping mid in the window.
  6. When lo equals hi, the number at that position is the smallest. Return it.

This Python version uses a while loop and compares the middle value with the value at hi.

find_min.py
def find_min(nums):
lo, hi = 0, len(nums) - 1
while lo < hi:
mid = lo + (hi - lo) // 2 # middle position
if nums[mid] > nums[hi]:
lo = mid + 1 # drop is to the right
else:
hi = mid # smallest is mid or left
return nums[lo] # lo meets hi at the smallest
nums = [4, 5, 6, 7, 0, 1, 2]
print(find_min(nums))

The output of the above code will be:

0

Let us walk through the Python version line by line, so the logic feels clear.

The line lo, hi = 0, len(nums) - 1 sets the two edges. lo is the first position and hi is the last. The smallest number is somewhere inside this window.

The line while lo < hi: keeps shrinking the window. Notice it is <, not <=. We stop the moment the two edges meet on one number, and that number is the answer.

The line mid = lo + (hi - lo) // 2 picks the middle position. We write it this way to stay safe from overflow in other languages, and it gives the same middle.

The line if nums[mid] > nums[hi]: is the heart of the trick. We compare the middle number with the rightmost number in our window. If the middle is bigger, the window is β€œbroken” on the right side. The drop must be after mid. So we set lo = mid + 1 and throw the left half away.

The else branch covers the case where the middle is smaller than or equal to the rightmost number. That means the right part is neat and sorted. The smallest value is at mid or to its left. So we set hi = mid. We keep mid itself, because mid might be the smallest.

When the loop ends, lo and hi point at the same spot. The line return nums[lo] hands back the smallest number.

⏱️ Time and Space Complexity

The linear scan looks at every number, so it is O(n). The binary search halves the window each step, so it finishes in about log n steps. Both use only a few variables, so the space is O(1).

Approach Time Complexity Space Complexity
Linear scan O(n) O(1)
Binary search on the drop O(log n) O(1)

Tip

The trick is comparing the middle with the rightmost number, not the leftmost. Comparing with hi tells you cleanly which half still holds the drop. Many people compare with lo and trip over edge cases.

🧩 Key Takeaways

  • βœ… A rotated sorted array is two sorted pieces with one drop between them.
  • βœ… The smallest number sits exactly at that drop point.
  • βœ… Compare the middle value with the value at hi to decide which half to keep.
  • βœ… Move hi to mid, not mid - 1, so you never skip the smallest value.
  • βœ… The window halves each step, giving O(log n) time.

Check Your Knowledge

4 questions Show quiz Hide quiz

Test what you learned. Pick an answer for each question, then click Check.

  1. 1

    What does a rotated sorted array look like?

    Why: A rotation moves a front chunk to the back, leaving two sorted pieces with a single drop point.

  2. 2

    In the binary search, what do we compare the middle value with?

    Why: Comparing nums[mid] with nums[hi] cleanly tells you which half still contains the drop.

  3. 3

    When nums[mid] is greater than nums[hi], what do you do?

    Why: A bigger middle means the drop is to the right, so you move lo past mid.

  4. 4

    Why do we set hi to mid instead of mid - 1?

    Why: The middle value could be the smallest, so we keep it in the window by setting hi to mid.

πŸš€ What’s Next?