Find Minimum in Rotated Sorted Array II, step by step

Find Minimum in Rotated Sorted Array II is solved by comparing the middle value with the rightmost value of the window: smaller means the minimum is at mid or to its left, larger means it is to the right, and equal means you can safely drop the right end by one. That is O(log n) on most inputs and O(n) in the worst case, because duplicates can hide where the rotation happened.

By Published 6 min read

The task in our own words

A non-decreasing list was rotated: some prefix was cut off and appended to the end. Values may repeat. Return the smallest value. The list is never empty. You return the value only, so which copy of a repeated minimum you land on does not matter.

Sample inputs written for this walkthrough
InputMinimumComment
[7, 9, 9, 2, 4, 7]2The drop is in the right half
[3, 3, 1, 3]1The minimum is surrounded by copies of one value
[4, 4, 4, 2, 3, 4]2Ends and middle look alike at the start
[1, 5, 5, 8]1Rotated by zero places, so it is simply sorted
[6, 6, 6]6Every value is the same
Sample inputs written for this walkthrough. Scroll sideways on a small screen.

Linear answers first

Calling the built-in minimum is O(n) and correct. A slightly smarter scan uses the rotation: walk forward until a value is smaller than the one before it. That value is the minimum. If no drop exists, the list is fully sorted and the first element wins. It is still linear but explains the structure, which helps you move to the faster idea.

def rotated_min_scan(vals):
    for i in range(1, len(vals)):
        if vals[i] < vals[i - 1]:
            return vals[i]
    return vals[0]
Find the drop: O(n) time, O(1) space.

Binary search against the right end

Keep a window [lo, hi] that always contains a copy of the minimum. Look at the middle value and compare it with vals[hi]. There are exactly three outcomes, and each one lets you cut the window safely.

What each comparison proves
ComparisonWhat it meansUpdate
vals[mid] < vals[hi]mid..hi is an ordered run, so nothing right of mid is smaller than vals[mid]hi = mid
vals[mid] > vals[hi]The drop lies between mid and hi, so the minimum is right of midlo = mid + 1
vals[mid] == vals[hi]Cannot tell which side holds the drophi = hi - 1
What each comparison proves. Scroll sideways on a small screen.

The third row is the duplicate case. Why is it safe to throw away vals[hi]? Because vals[mid] holds the same value and mid stays inside the window (mid is always less than hi while lo < hi). If vals[hi] happened to be the minimum, an equal copy survives at mid. You never lose the answer, you only lose speed.

def rotated_min(vals):
    lo, hi = 0, len(vals) - 1
    while lo < hi:
        mid = (lo + hi) // 2
        if vals[mid] < vals[hi]:
            hi = mid
        elif vals[mid] > vals[hi]:
            lo = mid + 1
        else:
            hi -= 1
    return vals[lo]
Python solution.
function rotatedMin(vals) {
  let lo = 0;
  let hi = vals.length - 1;
  while (lo < hi) {
    const mid = lo + Math.floor((hi - lo) / 2);
    if (vals[mid] < vals[hi]) {
      hi = mid;
    } else if (vals[mid] > vals[hi]) {
      lo = mid + 1;
    } else {
      hi -= 1;
    }
  }
  return vals[lo];
}
JavaScript solution.

Why not compare with the left end?

It feels symmetric, but it is not. If vals[mid] > vals[lo], the left part is ordered, yet the minimum could still be vals[lo] itself (when the list is not rotated) or somewhere right of mid (when it is). One comparison cannot separate those cases. The right end does not have this problem: a sorted run ending at hi always means the minimum is at mid or before it.

Walking through an example

Use [4, 4, 4, 2, 3, 4]. Start with lo 0 and hi 5. mid is 2 and both vals[2] and vals[5] are 4, so hi drops to 4. Now mid is 2 again, vals[2] is 4 and vals[4] is 3, larger, so lo becomes 3. mid is 3, vals[3] is 2, smaller than vals[4], so hi becomes 3. The window is a single slot and the answer is 2.

Complexity in detail

Cost by approach
TechniqueBest caseWorst caseSpace
Built-in min or full scanO(n)O(n)O(1)
Scan until the first dropO(1) if the drop is earlyO(n)O(1)
Binary search with right-end tiesO(log n)O(n)O(1)
Cost by approach. Scroll sideways on a small screen.

The worst case shows up on long runs of one value with a single smaller value tucked inside, like [5, 5, 5, 5, 4, 5, 5] stretched to thousands of 5s. Many comparisons are ties, and each tie moves hi only one step. That is not a flaw in the code. When a single smaller value hides among copies, any method has to look at most positions to find it.

Cases to run before you say done

  • A single element, where the loop is skipped.
  • A list with no rotation, like [1, 5, 5, 8]. Every comparison here is smaller-than, and hi walks left to index 0.
  • All values identical. Ties walk hi down to lo, which is fine.
  • The minimum at the last index, as in [4, 6, 6, 1].
  • Duplicates of the minimum on both sides of the drop, like [1, 1, 2, 1]. The value returned is 1, but see the mistakes below about indexes.

Pitfalls

  • Setting `hi = mid - 1` when vals[mid] is smaller. mid may be the minimum; keep it.
  • Treating the tie as "go left" or "go right". Going left fails on [3, 3, 1, 3], and going right fails on [3, 1, 3, 3, 3]. Shrinking by one is the only safe move.
  • Reusing the index as the rotation count. With duplicates, the slot you end on holds the minimum value but is not always where the original order began. In [1, 1, 2, 1] the search ends at index 0, while the original sorted list starts at index 3. If the interviewer wants the rotation point, add a check before each tie step: if vals[hi - 1] > vals[hi], then hi is the start.
  • Forgetting to state the O(n) worst case. It is the main difference from the version with distinct values.

How the explanation might sound

Fictional example

Answering the tie question under pressure
Interviewer
Why are you allowed to drop the last element when the middle and the end are equal?
Candidate
Because the middle holds the same value and stays in the window. If the end was the minimum, I still have a copy at mid. I lose one candidate position but never the answer value.
Candidate
The cost is that a list of mostly equal values can force n steps, so the worst case is linear even though typical inputs finish in log n.

Search in Rotated Sorted Array II is the twin of this problem: the same rotation, the same duplicate trap, but a target to find. Median of a Row-Wise Sorted Matrix shows binary search over values rather than positions. Kth Smallest Number in Multiplication Table pushes that value-space idea further. For a wider list of topics, read the algorithm interview questions.

Using Natively while you prepare

Natively can capture a screenshot of a practice problem and offer a suggested approach, which works well as a check after your own attempt. It also writes a transcript of a mock interview on your machine, so you can see whether your tie explanation came out as clearly as you thought. The AI interview assistant page explains the setup for Mac and Windows.

Questions people ask

How do you find the minimum in a rotated sorted array with duplicates?

Binary search comparing the middle value with the rightmost value. If it is smaller, keep the left part including mid; if larger, move past mid; if equal, shrink the right end by one.

Why compare with the right end instead of the left?

A sorted run ending at the right end always means the minimum is at mid or before it. Comparing with the left end cannot tell an unrotated list from a rotated one in a single check.

What is the worst-case time complexity?

O(n). When most values are equal, the middle and right end tie again and again, and each tie removes only one position. Typical inputs still finish in O(log n).

Does the search return the rotation index too?

Not reliably with duplicates. It returns the minimum value, but the index it stops on can be a different copy than where the original order began, so check for the drop explicitly if the index matters.

Where to go next

Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.

Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.

Keep reading

Put it on your own machine.

A live-conversation assistant that runs locally. Your conversations stay on your machine.

macOS 12+ · Windows 10/11

© 2026 Natively AI Private Limited

Source-available under the Natively Personal Use Source License v1.0 · IP geolocation by DB-IP