00:00

Problem Statement

Input/Output Examples

Visual Explanation

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.

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.

Complexity Analysis

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 ≤ in a[lo] ≤ a[mid]. When lo == 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.