Tutorialspoint
Problem
Solution
Submissions

Minimum in Rotated Sorted Array

Certification: Intermediate Level Accuracy: 0% Submissions: 0 Points: 10

Write a C program to find the minimum element in a rotated sorted array. The array was originally sorted in ascending order, then rotated at some pivot unknown to you beforehand. All elements in the array are unique. You must write an algorithm that runs in O(log n) time.

Example 1
  • Input: nums = [3, 4, 5, 1, 2]
  • Output: 1
  • Explanation: The original array was [1, 2, 3, 4, 5] rotated 3 times.
Example 2
  • Input: nums = [4, 5, 6, 7, 0, 1, 2]
  • Output: 0
  • Explanation: The original array was [0, 1, 2, 4, 5, 6, 7] rotated 4 times.
Constraints
  • n == nums.length
  • 1 ≤ n ≤ 5000
  • -5000 ≤ nums[i] ≤ 5000
  • All integers of nums are unique
  • nums is sorted and rotated between 1 and n times
  • Time Complexity: O(log n)
  • Space Complexity: O(1)
ArraysAlgorithmsEYZomato
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Use binary search since the array has rotated sorted property
  • Compare the middle element with the rightmost element
  • If middle element is greater than rightmost, minimum is in the right half
  • If middle element is less than rightmost, minimum is in the left half including middle
  • Continue until you find the minimum element
  • The minimum element is where the rotation occurs

Steps to solve by this approach:

 Step 1: Initialize left and right pointers to start and end of array
 Step 2: Check if array is not rotated (first element < last element), return first element
 Step 3: Use binary search to find the rotation point
 Step 4: Calculate mid point between left and right
 Step 5: If mid element > right element, minimum is in right half, move left pointer
 Step 6: If mid element < right element, minimum is in left half including mid, move right pointer
 Step 7: Continue until left equals right, return the element at that position

Submitted Code :