Search⌘ K
AI Features

Problem: Search in Rotated Sorted Array

Understand how to efficiently locate a target value in a rotated sorted array by applying a modified binary search. Explore identifying sorted array halves and narrowing search space to achieve logarithmic time complexity, ensuring optimal search performance.

Statement

You are given an integer array nums that was originally sorted in ascending order with all distinct values. This array may have been left rotated at some unknown pivot index k, transforming it into [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (using 00-based indexing). If no rotation was applied, the array remains in its original sorted order.

Given the array nums after this possible rotation and an integer target, return the index of target in nums if it exists, or return 1-1 if it does not.

Your solution must achieve O(logn)O(\log n) runtime complexity.

Note: The rotation described is a left rotation, meaning the first k elements are moved to the end of the array.

Constraints:

  • 11 \leq nums.length 5000\leq 5000 ...