Quick revision
The short answer
Searching a rotated sorted array is a binary search problem. When we cut any range at its middle, at least one half is still fully sorted, and comparing the target with that half's two end values tells us for certain whether to keep it or the other half. One binary search finds the target in O(log n) time with O(1) extra space.
The solution
What it costs
| Approach | Time | Extra space |
|---|---|---|
| Scan every element (brute force) | O(n) | O(1) |
| Ordinary binary search | O(log n), but wrong | O(1) |
| Find the drop, then search one run | O(log n), two searches | O(1) |
| One search using the sorted half | O(log n), one search | O(1) |
What to remember
We searched a rotated sorted array with one binary search by noticing that the single drop can lie in only one half, so the other half is sorted and its two ends tell us for certain whether the target is inside. That keeps the guarantee binary search needs, that the target is always between lo and hi, and it runs in O(log n) time with O(1) space. The clue to remember is that sorted data, even with its order broken in one known way, still calls for binary search, as long as one look at the middle can prove which half to drop.
1What a rotated sorted array is, and what we must find
Picture a shelf of files numbered in increasing order from left to right. Someone lifts the first few files off the left end and puts them back on the right end, without changing their order. The shelf now holds two sorted runs side by side. If you walked along it you would see the numbers climb, suddenly drop once, and then climb again. That single drop is the only trace of what was done.
Such an array is called a rotated sorted array. We start from the sorted array [3, 7, 9, 11, 15, 18, 22], cut it just before 15 and move the front part to the back, which gives [15, 18, 22, 3, 7, 9, 11]. The first run is 15, 18, 22, and the second is 3, 7, 9, 11. Given such an array and a target, we return the target's index, or -1 if it is absent, without knowing where the cut was made.
Let us keep this array and the target 7 for the whole article. By eye, 7 is at index 4, counting from 0. If the target were 18, the answer would be 1, and for 10 the answer would be -1, because 10 is not in the array.
The marked pair is the one place where a value is followed by a smaller one. The answer, index 4, is marked as found.
- run 1
- 15, 18, 22
- run 2
- 3, 7, 9, 11
- target
- 7
- answer
- 4
1The array is two sorted runs, 15 to 22 and 3 to 11, with one drop between 22 and 3. The target 7 is at index 4.
What to assume
- Are the values distinct?
- We assume they are.
- Might the array not be rotated at all?
- Yes, and the method must still work, because a sorted array is a rotation by zero.
- Can the array be empty?
- Yes, and then the answer is -1.
- Do we know where the drop is?
- No.
A careful candidate asks a few questions first. Are the values distinct? We assume they are, and a follow-up at the end explains why repeated values change the worst case. Might the array not be rotated at all? Yes, and the method must still work, because a sorted array is a rotation by zero. Can the array be empty? Yes, and then the answer is -1. Do we know where the drop is? No, and finding it is not required, though it is one possible route.
Your turn
Solve it before you read on. Above is one example with the result expected for it. Under What to assume are the points this solution takes as given. Say your approach aloud or type it below, and write the code in your own editor. Then take one hint at a time and compare.
2How to recognise that binary search still applies
A beginner reading this question often decides that the rotation has spoiled the order, so the safe thing is to look at every element. That is a reasonable first thought, but the question goes out of its way to tell us that the array was sorted, and in interview questions such a detail is almost never there by accident. Let us read the clues one at a time.
The first clue is the word sorted. Sorted data invites binary search, the method you use with a dictionary: open it in the middle, see whether your word comes before or after the page you are on, and throw away the half that cannot contain it. Each look halves what is left, which is why it is so fast.
The second clue is the word rotated. It tells us the order is broken, but only in one place. Most of the array is still in order, and the question for us is whether one look at the middle can still tell us, with certainty, which half to throw away.
The third clue is the task itself, to search for one element. We are not asked to sort the array or to list anything, only to say where one value is, and that is the kind of task binary search was made for.
So the problem belongs to the binary search family. The name comes from binary, meaning two, because each step cuts the remaining range into two halves and keeps one. The general rule to carry to new problems is that binary search does not need a perfectly sorted array. It needs a test at the middle that tells you, with certainty, which half cannot hold the answer. Whenever you can find such a test, the search takes logarithmic time.
Other patterns look possible but fit less well. A hash set answers is this value present in about one step, but building it needs a full pass, no better than the scan. Two pointers from both ends would still walk most of the array. Finding the drop first and then running plain binary search on the right run does work, and we will meet it as a middle step, but it takes two searches where one is enough.
3The honest first attempt, looking at every element
The brute force anyone would write first is a linear scan, and that is a perfectly good place to start. We walk from index 0 to the end, compare each element with the target, and return the index when they are equal. If we reach the end, the target is not there and we return -1. It does not care about rotation at all, which makes it easy to trust.
Let us count on the example. To find 7, the scan compares 15, 18, 22, 3 and then 7, five comparisons. To learn that 10 is missing it must compare all seven values, because it cannot stop early. For an array of n elements, the scan makes up to n comparisons, so the time is O(n), read as order n, meaning the work grows in step with the size. The extra memory, called the space, is O(1), because it keeps only the index i.
Where is the waste? The scan uses none of the order. When it sees 3 at index 3 and 7 at index 4, it already has enough information to know that 9 and 11 come next in order, and that nothing in the run 3 to 11 is bigger than 11. For a million elements it may make a million comparisons where about twenty would do.
So we would like to use binary search. But here is the question a beginner should ask: what goes wrong if we just run ordinary binary search on this array? Let us try it with the target 18 and see.
4Repairing binary search, one observation at a time
Ordinary binary search looks at the middle element and assumes that everything to its left is smaller and everything to its right is larger. In our array the middle element, at index 3, is 3. The target 18 is larger, so ordinary binary search throws away the left half, which is exactly where 18 lives. The picture shows it marching right and giving the wrong answer, and then shows our corrected rule finding 18 from the same first look.
The first three frames are ordinary binary search. The last two use the rule built in this section on the same array.
- target
- 18
- plain rule
- 18 > 3, go right
1Plain binary search for 18 looks at mid = 3 and sees 3. Since 18 is larger, it keeps only the right half, though 18 sits at index 1.
- target
- 18
- plain rule
- 18 > 9, go right
2In the right half it sees 9 at index 5, smaller than 18, so it goes right again, and then sees 11, smaller again.
- target
- 18
- result
- -1, wrong
3The range is now empty, so plain binary search answers -1, not present. It threw 18 away in its very first step.
- target
- 18
- sorted half
- right, 3 to 11
4Our rule looks at the same mid. a[lo] = 15 is not <= 3, so the right half 3 to 11 is sorted, and 18 is not inside 3 to 11.
- target
- 18
- result
- 1
5So it keeps the left half, hi = 2. Now mid = 1 and a[1] is 18, found at index 1.
So the trouble is that one look at the middle no longer tells us for certain where the target is. Let us find a test that does. The first observation is about the drop. The array has only one place where a value is followed by a smaller one. When we cut the range at mid, that drop falls either in the left half or in the right half, but not in both. So at least one half has no drop, and a half with no drop is fully sorted.
The second observation is that one comparison tells us which half is sorted. If a[lo] <= a[mid], the values climb all the way from lo to mid with no drop, so the left half is sorted. Otherwise the drop is somewhere between lo and mid, and the right half, from mid to hi, must be the sorted one. On our array, a[lo] is 15 and a[mid] is 3, so 15 <= 3 is false, and the right half 3, 7, 9, 11 is sorted.
A beginner often asks why the test uses <= and not <. Think of a range with only two elements, such as lo = 0 and hi = 1 in the array [3, 1]. Then mid equals lo, and a[lo] equals a[mid]. The left half is a single element, which is certainly sorted, and the <= says so. With < we would wrongly call the right half sorted.
The third observation is that in a sorted half we can tell for certain whether the target lies inside it, because we know its smallest and largest values: they are its two ends. If the left half is sorted, the target is in it exactly when a[lo] <= target < a[mid]. If the right half is sorted, the target is in it exactly when a[mid] < target <= a[hi]. The side next to mid is strict, because we have already checked a[mid] itself. If the target is inside the sorted half, we keep that half; if not, it can only be in the other half, so we keep that one.
Why not test the other half instead? Because the half with the drop holds values from two runs, so its ends do not bound what is inside: the left half 15, 18, 22, 3 has ends 15 and 3, yet it holds 22. Only a sorted half has ends we can trust. This is the step most people get wrong: they compare the target with whichever half they happen to look at first.
Why is the whole method correct? Before every step it is true that if the target is in the array at all, it lies between lo and hi. Each step keeps that true, because we only ever throw away a half that we have proved cannot contain the target. The range shrinks every step, so the loop ends, either with a[mid] equal to the target or with an empty range, which proves the target is absent.
5Watching the search find 7
The picture follows the search for 7 in [15, 18, 22, 3, 7, 9, 11]. Each round has three moments: we compute mid and look at a[mid], we decide which half is sorted, and we decide which half to keep. Elements outside the range from lo to hi are drawn faded, because they are out of play. The panel shows lo, hi and mid as numbers.
Faded elements are outside the range lo to hi. The marked block in a deciding frame is the half that is sorted.
- target
- 7
- lo
- 0
- hi
- 6
- mid
- -
1The search starts with the whole array in play, lo at index 0 and hi at index 6. If 7 is anywhere, it is between them.
- target
- 7
- lo
- 0
- hi
- 6
- mid
- 3
- a[mid]
- 3
2mid = 0 + (6 - 0) / 2 = 3, and a[3] is 3. That is not 7, so we must decide which half to keep.
- target
- 7
- lo
- 0
- hi
- 6
- mid
- 3
- sorted half
- right, 3 to 11
3Is a[lo] = 15 <= a[mid] = 3? No, so the drop lies in the left half and the right half, 3 7 9 11, is sorted.
- target
- 7
- lo
- 4
- hi
- 6
- mid
- -
- decision
- keep the right half
4Since 3 < 7 <= 11, the target must be inside the sorted right half, so lo = mid + 1 = 4. Indexes 0 to 3 are out of play.
- target
- 7
- lo
- 4
- hi
- 6
- mid
- 5
- sorted half
- left, 7 to 9
5mid = 4 + (6 - 4) / 2 = 5 and a[5] is 9. Is a[lo] = 7 <= 9? Yes, so the left half, 7 9, is sorted.
- target
- 7
- lo
- 4
- hi
- 4
- mid
- -
- decision
- keep the left half
6Since 7 <= 7 < 9, the target is inside the sorted left half, so hi = mid - 1 = 4. Only index 4 is still in play.
- target
- 7
- lo
- 4
- hi
- 4
- mid
- 4
- result
- 4
7mid = 4 + (4 - 4) / 2 = 4 and a[4] is 7, the target. The search returns 4 after looking at only three values.
Let us read the two decisions back. In the third and fourth frames the left half contains the drop, so we look at the right half, 3 to 11. Its ends bound everything in it, and 7 is between 3 and 11, so the target must be there, and in one step we throw away four elements.
In the fifth and sixth frames the range is 7, 9, 11, which has no drop at all, so the left half 7, 9 is sorted. Since 7 <= 7 < 9, the target is in it, and hi moves to 4. The final look at index 4 finds 7. Three looks in all, against five for the scan, and the gap widens quickly as the array grows.
6The Java solution, read line by line
Here is the complete program. main searches the example for a target in each run and for a missing one, then tries a single element, a two-element rotation, an array that was not rotated and an empty array. Each comment shows what that line prints.
Let us read search in the order it runs. lo and hi start at the two ends, so the whole array is in play. The while loop runs as long as the range from lo to hi holds at least one element. Inside it, mid is computed as lo + (hi - lo) / 2. It gives the same index as (lo + hi) / 2, but it cannot overflow when lo and hi are both large numbers near the limit of an int.
The first test, a[mid] == target, ends the search as soon as we land on the target. If we did not land on it, the next test, a[lo] <= a[mid], tells us which half is sorted. In the left-sorted branch we keep the left half by setting hi = mid - 1 when a[lo] <= target < a[mid], and otherwise we keep the right half with lo = mid + 1. The right-sorted branch is the mirror image, using a[mid] < target <= a[hi].
Notice that both moves step past mid, to mid - 1 or mid + 1. We already know a[mid] is not the target, and skipping it is what makes the range shrink every time. Writing hi = mid by mistake can loop forever on a two-element range. When the loop ends without finding the target, lo has passed hi, and we return -1.
7How much it costs, and the cases at the edges
Let us count on the example first. Finding 7 took three rounds, each with a few comparisons, and each round roughly halved the range: seven elements became three, then one. For n elements the number of halvings is about log2 of n, the number of times we can divide n by 2 before reaching 1. A million elements need about 20 rounds. So the time is O(log n).
The space is O(1). The method keeps three integers, lo, hi and mid, whatever the size of the array, and it does not call itself, so no call stack builds up.
| Approach | Time | Extra space |
|---|---|---|
| Scan every element (brute force) | O(n) | O(1) |
| Ordinary binary search | O(log n), but wrong | O(1) |
| Find the drop, then search one run | O(log n), two searches | O(1) |
| One search using the sorted half | O(log n), one search | O(1) |
Now the edge cases, each one run by main. An array that was never rotated always has its left half sorted, because a[lo] <= a[mid] holds everywhere, so the method behaves exactly like ordinary binary search and finds 3 at index 2 in [1, 2, 3]. A single element is compared directly and either found or not. An empty array starts with hi = -1, so the loop never runs and we return -1.
The two-element array [3, 1] with target 1 is worth tracing by hand. lo is 0, hi is 1, so mid is 0 and a[mid] is 3, not 1. The test a[lo] <= a[mid] compares 3 with itself and says the left half is sorted. Is 3 <= 1 < 3? No, so lo = 1. Now mid is 1, a[1] is 1, and we return 1. A missing target such as 10 shrinks the range until lo passes hi, and we return -1.
8Follow-up questions
What changes if the array may contain repeated values?
With repeated values, a[lo], a[mid] and a[hi] can all be equal, as in [2, 3, 2, 2, 2], and then we cannot tell which half is sorted: a[lo] <= a[mid] is true, yet the drop from 3 to 2 is on the left. The usual repair is to move lo forward by one, and hi back by one, when all three are equal, and try again. That keeps the search correct, but in an array where nearly every value is the same it may move one step at a time, so the worst case becomes O(n).
How do you find the smallest element, or how many times the array was rotated?
The smallest element sits just after the drop, and a binary search with a simpler test finds it. While lo < hi, if a[mid] > a[hi], the drop and the minimum are to the right of mid, so lo = mid + 1; otherwise the minimum is at mid or to its left, so hi = mid. When lo equals hi it is the index of the minimum. In our array that is index 3, and the index is also the number of places the sorted array was rotated to the right. It takes O(log n) time.
Would it be acceptable to find the drop first and then run ordinary binary search?
Yes. Once we know the index of the minimum, the array is two sorted runs, and comparing the target with the last element tells us which run to search. It is still O(log n) and some people find it easier to reason about. It simply takes two searches instead of one, so if the interviewer asks for one pass, the sorted-half rule is the one to give.
9Conclusion
We searched a rotated sorted array with one binary search by noticing that the single drop can lie in only one half, so the other half is sorted and its two ends tell us for certain whether the target is inside. That keeps the guarantee binary search needs, that the target is always between lo and hi, and it runs in O(log n) time with O(1) space. The clue to remember is that sorted data, even with its order broken in one known way, still calls for binary search, as long as one look at the middle can prove which half to drop.
Open with
Let me confirm the setup: the values are distinct, the array was sorted and then rotated by an unknown amount, possibly zero, and I return the index of the target or -1.
Then, if they want more
A linear scan works in O(n), but it ignores the order. Plain binary search fails, because the middle value no longer tells me which side the target is on. The fix rests on one fact: the array has a single drop, so when I cut the range at mid, at least one half is fully sorted.
If a[lo] <= a[mid], the left half is sorted; otherwise the right half is. For the sorted half I know its smallest and largest values, its two ends, so I can check whether the target falls in that range. If it does, I keep that half; if not, I keep the other.
I check a[mid] against the target first and always move lo or hi past mid, so the range shrinks every round. The invariant is that if the target exists, it lies between lo and hi. That gives O(log n) time and O(1) space.
I compute mid as lo + (hi - lo) / 2 to avoid overflow, and I would test an unrotated array, two elements and a missing target. With duplicates the worst case becomes O(n).
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 60 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 "Search for an element in a sorted array that has been rotated.": https://scrivio-interview-library.netlify.app/data-structures/solutions/search-for-an-element-in-a-sorted-array-that-has-been-rotated-e19304/
Coding patterns, question 6 of 15 Back to the path
More binary search questions