Java Array Program · A49
Search in a Rotated Sorted Array in Java
Even after rotation, at least one half around the middle is still sorted. Check whether the target lies inside that sorted half, and discard the other one.
Problem Statement
A sorted array of distinct integers has been rotated at an unknown point. Given a target, return its index, or -1 if it is not present.
The search must run in O(log n) time.
Input/Output Examples
[15, 18, 22, 3, 6, 9], target = 6Output4Explanation: 6 is in the second sorted part.[15, 18, 22, 3, 6, 9], target = 20Output-1Explanation: 20 would fit between 18 and 22 but is absent.[5], target = 5Output0Explanation: found at index 0.Visual Explanation
- lo=0, hi=5mid 22215..22 is sorted 6 not in 15..22: go right
- lo=3, hi=5mid 466 = target found
- Doneindex 4
15, 18, 22 is sorted but does not contain 6, so the search moves to the right half and finds 6 at the middle.Comparing a[lo] with a[mid] tells which half is sorted. A range check on that half decides where the target can be, so every step still halves the search space.
Approaches
The one-pass search decides the direction at every step. The two-phase search finds the rotation point first.
Approach 1: One-Pass Binary Search (Recommended)
Idea: at each step, find the sorted half and check whether the target lies within its bounds.
- Return
midif it holds the target. - If
a[lo] ≤ a[mid], the left half is sorted: go left ifa[lo] ≤ target < a[mid], else right. - Otherwise the right half is sorted: go right if
a[mid] < target ≤ a[hi], else left.
Time: O(log n) · Space: O(1).
Approach 2: Find the Pivot, Then Search
Idea: binary-search the smallest element (the rotation point), then run an ordinary binary search on the correct sorted part.
- While
lo < hi, moveloright ifa[mid] > a[hi], else movehitomid. - Search
[pivot, n)withArrays.binarySearch(). - If not found, search
[0, pivot).
Time: O(log n) · Space: O(1).
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: One-Pass Binary Search (Recommended)
One loop, and no need to know where the rotation happened.
import java.util.Arrays;
public class RotatedSearchOnePass {
// One half around mid is always sorted; check whether the target lies inside that half
static int search(int[] a, int target) {
int lo = 0;
int hi = a.length - 1;
while (lo <= hi) {
int mid = lo + (hi - lo) / 2;
if (a[mid] == target) {
return mid;
}
if (a[lo] <= a[mid]) { // left half is sorted
if (a[lo] <= target && target < a[mid]) {
hi = mid - 1;
} else {
lo = mid + 1;
}
} else { // right half is sorted
if (a[mid] < target && target <= a[hi]) {
lo = mid + 1;
} else {
hi = mid - 1;
}
}
}
return -1;
}
public static void main(String[] args) {
int[] a = {15, 18, 22, 3, 6, 9};
System.out.println(Arrays.toString(a) + ", target=6 -> " + search(a, 6));
System.out.println(Arrays.toString(a) + ", target=20 -> " + search(a, 20));
System.out.println("[5], target=5 -> " + search(new int[] {5}, 5));
}
}
[15, 18, 22, 3, 6, 9], target=6 -> 4 [15, 18, 22, 3, 6, 9], target=20 -> -1 [5], target=5 -> 0
Time complexity: O(log n) · Space complexity: O(1).
Approach 2: Find the Pivot, Then Search
Splits the problem into two familiar steps, which some find easier to get right.
import java.util.Arrays;
public class RotatedSearchPivot {
// Find the smallest element (the rotation point), then binary-search the correct sorted part
static int search(int[] a, int target) {
int lo = 0;
int hi = a.length - 1;
while (lo < hi) {
int mid = lo + (hi - lo) / 2;
if (a[mid] > a[hi]) {
lo = mid + 1;
} else {
hi = mid;
}
}
int pivot = lo;
int index = Arrays.binarySearch(a, pivot, a.length, target);
if (index >= 0) {
return index;
}
index = Arrays.binarySearch(a, 0, pivot, target);
return index >= 0 ? index : -1;
}
public static void main(String[] args) {
int[] a = {15, 18, 22, 3, 6, 9};
System.out.println(Arrays.toString(a) + ", target=6 -> " + search(a, 6));
System.out.println(Arrays.toString(a) + ", target=20 -> " + search(a, 20));
System.out.println("[5], target=5 -> " + search(new int[] {5}, 5));
}
}
[15, 18, 22, 3, 6, 9], target=6 -> 4 [15, 18, 22, 3, 6, 9], target=20 -> -1 [5], target=5 -> 0
Time complexity: O(log n) · Space complexity: O(1).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| One-Pass Binary Search | O(log n) | O(1) |
| Find the Pivot, Then Search | O(log n) | O(1) |
Here n is the array length. Both approaches halve the range at every step; the two-phase version does up to three binary searches.
Interview Tips and Recommended Approach
- Use
≤ina[lo] ≤ a[mid]. Whenlo == mid, the left "half" is one sorted element. - Check range bounds carefully. Off-by-one errors in the inclusive and exclusive ends are the usual bug.
- Distinct values matter. With duplicates, the worst case degrades to
O(n). - Know the related question: finding the minimum of a rotated array is the pivot search alone.
Recommended Approach: One-Pass Binary Search
Why: it keeps the logarithmic bound with a single loop and works without first locating the rotation point.