Scrivio Interview library
Binary searchSorting and searchingReported 17 times · 15 min read · updated 15 November 2026

A sorted array has been rotated by an unknown amount. Find its smallest element.

1A staircase with one drop in it

Imagine a staircase that climbs steadily, step after step, and then at one point drops suddenly to the floor before it begins to climb again. If you were asked to find the lowest step, you would not need to measure every step. You would only need to find the place where the staircase drops, because the lowest step is the first one after the drop. Our array is exactly such a staircase.

Let us see where the staircase comes from. Start with a sorted array, [3, 6, 9, 12, 15, 18, 22]. To rotate it by three places, we take its last three values, 15, 18 and 22, and move them, in the same order, to the front. That gives [15, 18, 22, 3, 6, 9, 12]. The question tells us the array was rotated, but not by how much, and asks for its smallest value.

Now look at the shape. The values climb from 15 to 22, which we call the left run, then fall to 3, then climb again from 3 to 12, the right run. A run here means a stretch of the array that is in increasing order. The smallest value sits exactly where the values drop, at the start of the right run. Here that is 3, at index 3. Let us check by hand: 15, 18 and 22 are all bigger than 3, and 6, 9 and 12 are bigger too. So the answer is 3.

PictureA staircase with one drop

Each bar is one value of the rotated array. The values climb, fall once, and climb again.

  1. 15182236912drop
    left run
    15, 18, 22
    right run
    3, 6, 9, 12
    minimum
    3

    1The array [15, 18, 22, 3, 6, 9, 12] climbs from 15 to 22, drops to 3, then climbs to 12. The smallest value sits right after the drop, at index 3.

What to assume

Are the values distinct?
We assume so here.
Can the array be empty?
We assume it has at least one value.
Do we return the value or its index?
The value.

There is one special case worth noticing now. If the array was rotated by zero places, or by its full length, it is simply sorted, with no drop at all, and the smallest value is the first one. Our solution must handle that case too, and it will turn out to be exactly where beginners go wrong.

A careful candidate asks a few questions before starting. Are the values distinct? We assume so here; repeated values change the problem, and a follow-up at the end explains how. Can the array be empty? We assume it has at least one value. Do we return the value or its index? The value, although the index falls out of the same search and equals the number of places the array was rotated.

2How to recognise that binary search fits here

A beginner reading this question for the first time usually thinks: finding the smallest value means looking at every value. For an ordinary jumbled array that is true, and there is nothing wrong with starting there. But this array is not jumbled. The question goes out of its way to say it was sorted before it was rotated, and an interviewer does not mention such things by accident.

The first clue is the phrase sorted array. Even after the rotation, the array is made of two sorted runs, so it is nearly sorted, and most of the order is still there to be used. Sorted data lets one comparison tell us about many values at once.

The second clue is the word find, with a single answer at a special position, the drop. When an answer sits at one position and we can tell, by looking at a single value, whether that position is to its left or its right, we do not have to look at everything.

Those clues point to binary search, which means searching by splitting the candidates in two. You know it from the game where a friend thinks of a number from 1 to 100 and answers only higher or lower; each guess throws away half the numbers that remain. The general rule to carry to new problems is that binary search does not need a fully sorted array. It needs only a test on the middle element that tells us, with certainty, which half cannot contain the answer. Here the test will be one comparison between the middle value and the value at the right end of the range.

Some patterns look similar but do not fit. Two pointers and the sliding window also move indexes along an array, but they move one step at a time, so they are linear. A heap, or sorting a copy, would find the minimum of any array, but both ignore the order we were given and cost more than a plain scan.

3The honest first attempt, a brute force scan of every value

The first attempt anyone would make, the brute force, is to walk along the array, keep the smallest value seen so far, and return it at the end. It works for every array, rotated or not, which makes it a safe starting point.

Java
// Brute force: look at every element and keep the smallest.
static int findMin(int[] a) {
int min = a[0];
for (int x : a) min = Math.min(min, x);
return min;
}

Let us run it by hand. min starts as 15. Then 18 and 22 are bigger, so min stays 15. Then 3 is smaller, so min becomes 3, and 6, 9 and 12 leave it at 3. We looked at all seven values to return 3.

For an array of n values the loop runs n times, so the time is O(n), read as order n, meaning it grows in step with the length. The extra space is O(1), constant, just the one variable min.

A slightly smarter scan stops at the first value that is smaller than the one before it, since that is the drop. On our example it stops at index 3, after four values. But if the drop is near the end, it still reads almost everything, so in the worst case it is O(n) too.

So where is the waste? Inside a sorted run, we already know the order before we look. Once we know that 15, 18 and 22 form a climbing run, reading 18 and 22 tells us nothing new about the minimum. The scan pays to read values whose order the question has already promised us. All we need is to locate the drop, and the next section shows that a single comparison can rule out half the array.

4Finding the drop with one comparison at a time

Let us keep two indexes, lo and hi, around the part of the array that may still hold the minimum. At first they cover everything, lo = 0 and hi = 6. We look at the middle index, mid, and ask one question: is the drop to the right of mid, or is the minimum at mid or to its left? To answer it, we compare a[mid] with a[hi], the value at the right end of the range. The picture shows the two possible answers and one trap.

Step throughWhat one comparison with the right end tells us

mid is the middle of the range and hi is its right end. The comparison of their values tells us which side holds the drop.

  1. 15182236912midhi
    a[mid]
    18
    a[hi]
    12
    so
    drop is right of mid

    1Here a[mid] = 18 is bigger than a[hi] = 12. Values never go down inside a sorted run, so the drop must lie somewhere after mid.

  2. 15182236912midhi
    a[mid]
    6
    a[hi]
    12
    so
    minimum is mid or left

    2Here a[mid] = 6 is smaller than a[hi] = 12. The stretch from mid to hi has no drop inside it, so its smallest value is a[mid] itself.

  3. 36912151822lomid
    a[mid]
    12
    a[lo]
    3
    trap
    points the wrong way

    3Comparing with the left end instead fails on an array that was not rotated: a[mid] = 12 > a[lo] = 3 seems to say go right, yet the minimum is at index 0.

Take the first frame. a[mid] = 18 is bigger than a[hi] = 12. If the stretch from mid to hi were one sorted run, the later value a[hi] could not be smaller. So the values must drop somewhere after mid, and the minimum is after mid as well. mid itself cannot be the minimum either, because a[hi] is smaller than it. So we set lo = mid + 1.

Now the second frame. a[mid] = 6 is smaller than a[hi] = 12. Could the drop be hidden between them? If it were, mid would sit in the left run and hi in the right run, and every value in the left run is bigger than every value in the right run, so a[mid] would be bigger than a[hi]. It is not, so the stretch from mid to hi has no drop and is sorted, its smallest value is a[mid], and the minimum of the whole range is mid or something to its left. We set hi = mid, keeping mid, because it may be the answer.

Here the beginner's question is a fair one: why does lo become mid + 1 but hi become just mid, not mid - 1? The two cases rule out different things. When a[mid] > a[hi], we know mid is in the left run and cannot be the minimum, so it is safe to drop it. When a[mid] < a[hi], mid may well be the minimum itself, as it will be in the very first step of our example. Writing hi = mid - 1 there is the most common mistake in this problem, and it skips the answer.

The third frame answers another natural question: why compare with the right end and not the left? It is tempting to compare a[mid] with a[lo], and it seems symmetric. But on an array that was not rotated, a[mid] = 12 is bigger than a[lo] = 3, which looks just like the first case and would send us right, away from the minimum at index 0. The right end has no such trap. Whenever the range still contains the drop, a[hi] lies in the right run, below everything in the left run, and when the range has no drop at all, every a[mid] is smaller than a[hi], so hi simply walks left towards the first value.

We repeat while lo < hi. Each step keeps the minimum inside lo to hi and makes the range smaller, so when lo and hi finally meet, the single index left must hold the minimum.

5Watching the range shrink on the example

The picture plays the search on [15, 18, 22, 3, 6, 9, 12]. Under the cells, lo, mid and hi show where the three indexes are, and dimmed cells have been ruled out. The panel shows the test made in each step.

Watch how each comparison throws away a whole block of cells, sometimes from the right and sometimes from the left.

Step throughBinary search for the minimum of [15, 18, 22, 3, 6, 9, 12]

lo and hi bound the cells that may still hold the minimum. Dimmed cells have been ruled out.

  1. 150181222336495126lohi
    lo
    0
    hi
    6

    1The minimum could be anywhere, so lo = 0 and hi = 6 cover the whole array.

  2. 150181222336495126lomidhi
    lo
    0
    hi
    6
    mid
    3
    test
    3 < 12

    2mid = 0 + (6 - 0) / 2 = 3. We compare a[mid] = 3 with a[hi] = 12, and 3 < 12, so the stretch from 3 to 6 is sorted.

  3. 150181222336495126lohi
    lo
    0
    hi
    3
    rule
    hi = mid

    3The minimum is mid or to its left, so hi = mid = 3. We keep index 3, because it might be the answer, and drop indexes 4 to 6.

  4. 150181222336495126lomidhi
    lo
    0
    hi
    3
    mid
    1
    test
    18 > 3

    4mid = 0 + (3 - 0) / 2 = 1. We compare a[mid] = 18 with a[hi] = 3, and 18 > 3, so the drop lies after mid.

  5. 150181222336495126lohi
    lo
    2
    hi
    3
    rule
    lo = mid + 1

    5Index 1 cannot be the minimum, since a[hi] is smaller, so lo = mid + 1 = 2. Indexes 0 and 1 are ruled out.

  6. 150181222336495126lomidhi
    lo
    2
    hi
    3
    mid
    2
    test
    22 > 3

    6mid = 2 + (3 - 2) / 2 = 2. We compare a[mid] = 22 with a[hi] = 3, and 22 > 3, so the drop is still to the right.

  7. 150181222336495126lohi
    lo
    3
    hi
    3
    loop
    ends

    7lo = mid + 1 = 3. Now lo equals hi, so the test lo < hi fails and the loop ends with one candidate left.

  8. 150181222336495126lohi
    return
    3
    comparisons
    3

    8We return a[lo] = a[3] = 3, the smallest value, after three comparisons. Its index, 3, is also how far the array was rotated.

Let us read the important frames back. In the second frame mid lands on 3, which happens to be the answer, and 3 < 12 tells us the stretch from index 3 to 6 is sorted. In the third frame we set hi = 3, not 2, and that is what keeps the answer in play. With hi = mid - 1 the answer would be gone already.

In the fourth and sixth frames the middle values are 18 and 22, both bigger than a[hi] = 3, so the drop is to their right and lo moves past them each time. After three comparisons lo and hi meet at index 3 and we return 3. Notice that the final index, 3, is also the number of places the array was rotated.

6The complete Java program

Here is the whole program. main runs our example, the commonly used [4, 5, 6, 7, 0, 1, 2], an array that was not rotated, a two-value array and a single value, with each printed result shown as a comment.

Java
public class RotatedMin {
// Smallest element of a sorted array of distinct values rotated by an unknown amount.
static int findMin(int[] a) {
int lo = 0, hi = a.length - 1;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (a[mid] > a[hi]) {
lo = mid + 1; // the drop is to the right of mid, so the minimum is too
} else {
hi = mid; // mid..hi is sorted: the minimum is mid or to its left
}
}
return a[lo]; // lo == hi: the only candidate left
}
public static void main(String[] args) {
System.out.println(findMin(new int[] {15, 18, 22, 3, 6, 9, 12})); // 3
System.out.println(findMin(new int[] {4, 5, 6, 7, 0, 1, 2})); // 0
System.out.println(findMin(new int[] {1, 2, 3, 4, 5})); // 1
System.out.println(findMin(new int[] {2, 1})); // 1
System.out.println(findMin(new int[] {7})); // 7
}
}

Let us read findMin in the order it runs. lo and hi start at the two ends of the array. The loop runs while lo < hi, that is, while more than one candidate remains.

Inside the loop, mid is written as lo + (hi - lo) / 2 rather than (lo + hi) / 2. Both give the same index here, but for very large arrays lo + hi could exceed the largest int and wrap around to a negative number, and the first form cannot.

Then comes the one comparison. If a[mid] > a[hi], the drop is to the right and mid is not the minimum, so lo = mid + 1. Otherwise the stretch from mid to hi is sorted, so hi = mid. Because mid is always strictly less than hi while lo < hi, both branches shrink the range, so the loop always ends.

When it ends, lo and hi are equal, and a[lo] is the only candidate left, so we return it. Why not loop while lo <= hi, as in an ordinary binary search? Because when lo equals hi, mid equals hi too, the else branch sets hi = mid, nothing changes, and the loop would run forever. On our inputs main prints 3, 0, 1, 1 and 7.

7How much it costs, and the cases at the edges

Let us count on the example first. The range held 7 candidates, then 4, then 2, then 1, which took three comparisons, against seven reads for the scan. Each step keeps about half of the range, so for n values the loop runs about log2 n times, which we write as O(log n). For a million values that is about 20 comparisons instead of a million. The extra space is O(1), three ints, whatever the length.

Comparing the ways to find the minimum
ApproachTimeExtra space
Scan every valueO(n)O(1)
Scan until the first dropO(n) worst caseO(1)
Binary search against a[hi]O(log n)O(1)

Now the edges, each of which main tests. An array that was not rotated, [1, 2, 3, 4, 5], has every a[mid] smaller than a[hi], so hi keeps moving to mid until it reaches 0, and we return 1, the first value. A two-value array [2, 1] has mid = 0; 2 > 1, so lo = 1 and we return 1. A single value skips the loop and is returned at once.

An empty array would make a[lo] throw an exception, so we either agree with the interviewer that it cannot happen, as we did, or check for it at the start and report it clearly.

8Follow-up questions

What changes if the array may contain repeated values?

When a[mid] equals a[hi], one comparison can no longer tell us which side holds the drop. In [3, 3, 1, 3] and [3, 1, 3, 3], the middle and right values are both 3, yet the minimum is on different sides. The safe move is hi--, dropping only the right end: even if a[hi] was the minimum, a[mid] has the same value and stays in the range. The other two cases stay as they are. The answer stays correct, but when nearly every value is equal, the search can only drop one value per step, so the worst case becomes O(n).

How would you search for a target value in a rotated sorted array?

The idea of a sorted stretch carries over. At each step, at least one of the halves lo to mid and mid to hi is sorted, which we can tell by comparing a[lo] with a[mid]. If the target lies between the end values of the sorted half, we search there; otherwise we search the other half. That is O(log n) too. Another way is to find the minimum's index with this method first, then run an ordinary binary search on whichever run could hold the target.

How do you find how many times the array was rotated?

The number of rotations is the index of the minimum, because rotating by k places moves the original first value, the smallest, to index k. So we run the same search and return lo instead of a[lo]. For our example that is 3, and for an array that was not rotated it is 0.

9Conclusion

We found the smallest value of a rotated sorted array by looking for the drop in the staircase. A scan read every value even though each run was already sorted, so we compared the middle value with the right end of the range: a larger middle means the drop is to its right, a smaller one means the minimum is the middle or to its left. Halving the range each time gives O(log n) time and O(1) space. The clue to remember is that binary search needs only a test on the middle element that rules out half the candidates, not a fully sorted array.

What to say in the interviewAbout 90 seconds aloud

Open with

Let me check my assumptions: the array is not empty, the values are distinct, and I return the minimum value. A linear scan works in O(n), but it ignores that the array is made of two sorted runs.

Then, if they want more

The minimum is exactly where the values drop, and one comparison tells me which side of the middle that drop is on, so I will use binary search. I keep lo and hi around the range that still holds the minimum.

I take mid as lo plus half the distance to hi, and compare a of mid with a of hi. If a of mid is bigger, the values must drop somewhere after mid and mid itself cannot be the minimum, so lo becomes mid plus one.

Otherwise the stretch from mid to hi is sorted, so the minimum is mid or to its left, and hi becomes mid, keeping mid as a candidate. I loop while lo is less than hi and return a of lo.

I compare with the right end rather than the left, because the left end points the wrong way on an array that was not rotated. It is O(log n) time and O(1) space. I would test an unrotated array, two values and one value.

With duplicates, I would decrement hi when the two values are equal, which makes the worst case linear.

About this answer

Last updated
15 November 2026
Written for
Java 21, using only java.util
Checked
The Java solution was compiled and run, and each printed result matches the comment beside it. The pictures are checked against the format when the site is built. The explanation around the code is a study note: confirm anything critical against the documentation below. What the checks cover
Where the question comes from
Public interview write-ups, reworded by Scrivio; counted 17 times in that collection. How it is counted
Official documentation
java.util, package documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "A sorted array has been rotated by an unknown amount. Find its smallest element.": https://scrivio-interview-library.netlify.app/data-structures/solutions/a-sorted-array-has-been-rotated-by-an-unknown-amount-find-its-sm-aba507/