Find minimum in Rotated Sorted Array

Binary Search Logic Building Easy

Given an integer array nums of size N, sorted in ascending order with distinct values, and then rotated an unknown number of times (between 1 and N), find the minimum element in the array.

Examples:

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

Output: 0

Explanation: Here, the element 0 is the minimum element in the array.

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

Output: 1

Explanation:Here, the element 1 is the minimum element in the array.

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

Constraints

  • n == nums.length
  •  1 <= n <= 104
  •  -104 <= nums[i] <= 104
  •  All the integers of nums are unique.
  •  nums is sorted and rotated between 1 and n times.

Hints

  • A rotated sorted array contains two sorted segments, with the rotation causing a discontinuity. The smallest element (minimum) is the pivot point where the rotation occurs. The minimum element is the only one smaller than its previous element (except at index 0, where it is compared to the last element).
  • "Compare the middle element (nums[mid]) with the rightmost element (nums[high]): If nums[mid] > nums[high], the minimum lies in the right half (low = mid + 1). If nums[mid] <= nums[high], the minimum lies in the left half (high = mid)."

Company Tags

Ernst & Young Nutanix Red Hat Optum HashiCorp Philips Healthcare DoorDash Target Ubisoft Zomato Airbnb Reddit KPMG Morgan Stanley OYO Rooms Zynga Snowflake Databricks IBM Uber Siemens Healthineers Splunk Shopify American Express Twilio TCS Cognizant Accenture Infosys Capgemini Wipro