Find Minimum in Rotated Sorted Array - Leetcode 153

I'm a developer who loves to write. I started my blog because I wanted to share my knowledge and passion for technology with others. I write about a variety of topics, including coding, web development, and blockchain.
Problem - Leetcode
Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:
[4,5,6,7,0,1,2]if it was rotated4times.[0,1,2,4,5,6,7]if it was rotated7times.
Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].
Given the sorted rotated array nums of unique elements, return the minimum element of this array.
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] and it was rotated 4 times.
Example 3:
Input: nums = [11,13,15,17]
Output: 11
Explanation: The original array was [11,13,15,17] and it was rotated 4 times.
Constraints:
n == nums.length1 <= n <= 5000-5000 <= nums[i] <= 5000All the integers of
numsare unique.numsis sorted and rotated between1andntimes.
Solution in Golang
func findMin(nums []int) int {
res := nums[0]
left, right := 0, len(nums)-1
for left <= right {
mid := (left + right) / 2
if nums[mid] >= nums[0] {
left = mid + 1
} else {
if nums[mid] < res {
res = nums[mid]
}
right = mid - 1
}
}
return res
}
This Go code defines a function findMin that takes a sorted and rotated array of integers nums as input and returns the minimum element in the array. The array is sorted but may have been rotated, meaning elements have been moved to the left or right.
Let's break down the code step by step:
res := nums[0]: Initializes the result variablereswith the first element of the array. This assumes that the array is not empty.left, right := 0, len(nums)-1: Initializes two pointers,leftandright, which represent the range of elements currently being considered.leftstarts at the beginning of the array, andrightstarts at the end.for left <= right {: Initiates a loop that continues as long as theleftpointer is less than or equal to therightpointer.mid := (left + right) / 2: Calculates the middle index of the current range.if nums[mid] >= nums[0] {: Checks if the middle element is greater than or equal to the first element of the array. This condition is used to determine whether the rotation point is on the right side of the array.If true, it means the minimum element is on the right side, so the
leftpointer is updated tomid + 1.If false, it means the rotation point, and consequently, the minimum element, is on the left side or at the current position. In this case, it goes to the
elseblock.
if nums[mid] < res {: Checks if the current element at the middle index is less than the current minimum (res).- If true, it updates the minimum (
res) to the value at the middle index.
- If true, it updates the minimum (
right = mid - 1: Adjusts therightpointer to continue searching on the left side of the array.The loop continues until the
leftpointer exceeds therightpointer.return res: Returns the minimum element found during the search.
In summary, this code efficiently finds the minimum element in a sorted and rotated array using a binary search approach. The algorithm exploits the sorted nature of the array and adapts the search based on the comparison between the middle element and the first element of the array.



