Search in Rotated Sorted Array - JS Solution

JS
Editorial

Solution explanation

Solution explanation

Approach

Approach

Problem

Given a rotated sorted array of distinct integers and a target, return the index of the target or -1 if not found, in O(log n) time.

Pointers

  • left β€” start index of the current search range
  • right β€” end index of the current search range
  • mid β€” middle index: Math.floor((left + right) / 2)

Segments in a Rotated Array

A rotation of a strictly increasing array splits it into two sorted segments:

  • First sorted segment β€” the left portion (before the rotation point) whose values are all greater than every value in the second segment. This segment can be empty if the array is not rotated (fully increasing).
  • Second sorted segment β€” the right portion (from the rotation point to the end) that is strictly increasing. This segment can be the entire array if there is no rotation.

Key Idea

During each binary-search step we follow three checks:

  • βœ… First, if nums[mid] === target, we immediately return mid.
  • Otherwise, compare nums[mid] with nums[right] to decide which sorted segment mid belongs to, and therefore which half is assured to be sorted.

How we classify mid

  • If nums[mid] ≀ nums[right], then mid is in the second sorted segment, because every value in the first sorted segment is greater than nums[right].
    Therefore, the right half ([mid..right]) is assured to be sorted.
  • Otherwise (if nums[mid] > nums[right]), mid is in the first sorted segment, because no value in the second sorted segment can exceed nums[right].
    Therefore, the left half ([left..mid]) is assured to be sorted.

Range Check on the Half Assured to Be Sorted

After identifying the assured-sorted half, we check whether the target lies within its bounds:

  • If the right half is assured-sorted (nums[mid] ≀ nums[right]):
    If the target lies within bounds (nums[mid], nums[right]], continue on the right: left = mid + 1.
    Otherwise, search the other half: right = mid - 1.
  • If the left half is assured-sorted (nums[mid] > nums[right]):
    If the target lies within bounds [nums[left], nums[mid]), continue on the left: right = mid - 1.
    Otherwise, search the other half: left = mid + 1.

Why This Works

  • nums[right] serves as a boundary: all values in the second segment are ≀ nums[right], all values in the first segment are > nums[right].
  • Comparing nums[mid] with nums[right] reveals which segment mid belongs to, so one half is assured to be sorted.
  • Once a half is assured sorted, a simple range check decides whether to continue in that half or switch to the other, maintaining O(log n) complexity.

Time Complexity

O(log n)
Each step of binary search discards half of the remaining range by updating either left or right. The number of iterations is therefore logarithmic in the array length n.

Space Complexity

O(1)
Only a constant number of variables (left, right, mid) are used, regardless of input size.

Visucodize editorial solution titled JS Solution for LeetCode's Search in Rotated Sorted Array coding problem. Includes code and may include explanation. You can animate the code step by step using the provided input by clicking the run button, or fork it locally to update the code and input for custom visualization. View the original problem at https://leetcode.com/problems/search-in-rotated-sorted-array.