[Animated Video] Simple, visual solution
CodeInMotion ResourcesEvery Leetcode Pattern You Need To Knowhttps://www.blog.codeinmotion.io/p/leetcode-patternsBlind 75 Animated Playlisthttps://www.youtube.c
![Code In Motion Purple Transparent Background (2).png](https://assets.leetcode.com/users/images/69a60a6b-9c0f-4aa1-899b-98ee5f9b1dc1_1733778334.1189735.png)

# *CodeInMotion Resources*
## Every Leetcode Pattern You Need To Know
https://www.blog.codeinmotion.io/p/leetcode-patterns

## Blind 75 Animated Playlist
https://www.youtube.com/playlist?list=PLHm8nzcbp3_19DiTlDg8QYvR-hN5jzPCp

## Subscribe to YouTube
https://www.youtube.com/@CodeInMotion-IO?sub_confirmation=1

---
## Animated Video
https://youtu.be/5AM_xIetqvQ

## Intuition
Rotating a sorted array means that some pivot splits the array into two sorted subarrays. Our goal is to find the smallest element, which effectively is the pivot point. The key observation is that the smallest element is always on the “lesser” side during the rotation. By leveraging binary search and comparing elements with the right boundary, we can discard half of the search space at a time until we narrow down to the minimum element.

## Approach
1. Initialize two pointers, **left** at index 0 and **right** at the end of the array.
2. While **left** is less than **right**:
   - Compute **mid** as the average of **left** and **right**.
   - If the **nums[mid]** is less than or equal to **nums[right]**, it means the minimum could be at **mid** or to the left of **mid**, so set **right = mid**.
   - Otherwise, it means the minimum is to the right of **mid**, so set **left = mid + 1**.
3. Once the loop finishes, the **left** pointer will be pointing to the minimum element. Return **nums[left]**.

## Complexity
- Time complexity: $$O(\log n)$$
- Space complexity: $$O(1)$$

## Code
```Python []
class Solution:
    def findMin(self, nums: List[int]) -> int:
        left, right = 0, len(nums) - 1
        while left < right:
            mid = left + (right - left) // 2
            if nums[mid] <= nums[right]:
                right = mid
            else:
                left = mid + 1
        return nums[left]
```
```Java []
class Solution {
    public int findMin(int[] nums) {
        int left = 0;
        int right = nums.length - 1;
        
        while (left < right) {
            int mid = left + (right - left) / 2;
            
            if (nums[mid] <= nums[right]) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        
        return nums[left];
    }
}
```
```JavaScript []
/**
 * @param {number[]} nums
 * @return {number}
 */
var findMin = function(nums) {
    let left = 0, right = nums.length - 1;
    
    while (left < right) {
        let mid = Math.floor(left + (right - left) / 2);
        
        if (nums[mid] <= nums[right]) {
            right = mid;
        } else {
            left = mid + 1;
        }
    }
    
    return nums[left];
};
```
```C# []
public class Solution {
    public int FindMin(int[] nums) {
        int left = 0;
        int right = nums.Length - 1;
        
        while (left < right) {
            int mid = left + (right - left) / 2;
            
            if (nums[mid] <= nums[right]) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        
        return nums[left];
    }
}
```
```C++ []
class Solution {
public:
    int findMin(vector<int>& nums) {
        int left = 0;
        int right = nums.size() - 1;
        
        while (left < right) {
            int mid = left + (right - left) / 2;
            
            if (nums[mid] <= nums[right]) {
                right = mid;
            } else {
                left = mid + 1;
            }
        }
        
        return nums[left];
    }
};
```
```Go []
func findMin(nums []int) int {
    left, right := 0, len(nums) - 1
    
    for left < right {
        mid := left + (right - left) / 2
        
        if nums[mid] <= nums[right] {
            right = mid
        } else {
            left = mid + 1
        }
    }
    
    return nums[left]
}
```

### Please thumbs up on the bottom-left corner!<br>It motivates me to create more solutions and videos for you!
![thumbs up (1).png](https://assets.leetcode.com/users/images/f69ffdba-12f7-4f5b-8950-e7d3bd77c52a_1733768002.4687448.png)
['Array', 'Binary Search', 'Python', 'C++', 'Java', 'Go', 'Python3', 'JavaScript', 'C#']