MediumArrayBinary Search

Find Minimum in Rotated Sorted Array

LeetCode
1 approach, code in all languages

Imagine an array that was originally sorted in ascending order and then rotated to the left some unknown number of times, so a slice of the front got moved to the back. Every element is unique. Your job is to return the smallest value that remains hidden somewhere inside this rotated arrangement.

A sweep across all elements would obviously work, but the array's structure lets you discard half of the remaining candidates at each step, so an efficient solution should run in logarithmic time.

Example 1

Input: nums = [3,4,5,1,2]

Output: 1

The original [1,2,3,4,5] was rotated so the minimum 1 landed in the middle.

Example 2

Input: nums = [4,5,6,7,0,1,2]

Output: 0

After rotation the smallest value 0 sits at index 4.

Constraints

  • n == nums.length
  • 1 ≤ n ≤ 5000
  • -5000 ≤ nums[i] ≤ 5000
  • All integers in nums are unique
You've got the patterns

Patterns get you through the screen. Shipping gets you hired.

FDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.

750+ engineers trained · frontend, backend, AWS & AI

August 15 · 0d left
Enroll Now