Fastest (100%) || Easiest || Shortest || Binary Search || Multiple Langs
\n\n# Approach\nPicture this: You\'re exploring a treasure-filled island, but the path to the treasure is tricky\u2014it\'s like a maze. Now, this approach is l
\n\n# Approach\nPicture this: You\'re exploring a treasure-filled island, but the path to the treasure is tricky\u2014it\'s like a maze. Now, this approach is like having a smart compass that helps you navigate through the maze efficiently.\n\nHere\'s how Franky (from OnePiece) would break it down:\n\n1. **Setting sail**: You start with two markers, one at the beginning of the path (let\'s call it "left") and one at the end (that\'s "right"). These markers help you keep track of where you are.\n\n2. **Sailing through the maze**: You sail through the maze using a special technique called "binary search". It\'s like cutting the maze in half each time to find the quickest route to the treasure.\n\n3. **Calculating the midpoint**: At each intersection, you calculate the midpoint to decide which direction to go next.\n\n4. **Reading the signs**: Now, here\'s the trick\u2014when you reach an intersection, you check the signs. If the sign at the midpoint points toa smaller number than the one at the end of the path, you know the treasure is in the left half. So, you adjust your right marker to the midpoint.\n\n5. **Deciding the route**: But if the sign at the midpoint suggests the number is larger than the one at the end of the path, it means the treasure is likely in the right half. So, you move your left marker to just after the midpoint.\n\n6. **Narrowing down**: You keep repeating this process, narrowing down your search until your markers converge.\n\n7. **Treasure found!**: When your markers meet, it means you\'ve found the smallest number, which is like discovering the treasure in the maze. That\'s the number you return\u2014it\'s the minimum value in the rotated array.\n\nSo, with this clever compass-like approach, you efficiently navigate through the maze of numbers and find your treasure in no time! That\'s the spirit of adventure, just like sailing with the Straw Hat Pirates!\n\n\n# Dry - Run\nLet\'s dry run the provided solution with the example `nums = [4,5,6,7,0,1,2]`:\n\nInitial state:\n- `left = 0`, `right = 6`.\n- `mid = 3`, `nums[mid] = 7`, `nums[right] = 2`.\n\nSince `nums[mid] >= nums[right]`, we update `left = mid + 1 = 4`.\n\nAfter the first iteration:\n- `left = 4`, `right = 6`.\n- `mid = 5`, `nums[mid] = 1`, `nums[right] = 2`.\n\nSince `nums[mid] < nums[right]`, we update `right = mid = 5`.\n\nAfter the second iteration:\n- `left = 4`, `right = 5`.\n- `mid = 4`, `nums[mid] = 0`, `nums[right] = 1`.\n\nSince `nums[mid] < nums[right]`, we update `right = mid = 4`.\n\nAfter the third iteration:\n- `left = 4`, `right = 4`.\n\nThe loop stops as `left` is no longer less than `right`, and the algorithm returns `nums[left]`, which is `0`.\n\nSo, for the input `nums = [4,5,6,7,0,1,2]`, the output of the algorithm is `0`, which is correct. \n\n# Complexity\n- Time complexity: O(logn)\n\n- Space complexity: O(1)\n\n# Code\n```C++ []\n#pragma GCC optimize ("Ofast")\n#pragma GCC target ("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")\n#pragma GCC optimize ("-ffloat-store")\n\nclass Solution {\npublic:\n    int findMin(vector<int>& nums) {\n        ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);\n        int left = 0, right = nums.size() - 1;\n\n        while(left < right) {\n            int mid = left + (right - left) / 2;\n            if(nums[mid] < nums[right]) {\n                right = mid;\n            } else {\n                left = mid + 1;\n            }\n        }\n\n        return nums[left];\n    }\n};\n```\n```java []\nclass Solution {\n    public int findMin(int[] nums) {\n        int left = 0, right = nums.length - 1;\n        while(left < right) {\n            int mid = left + (right - left) / 2;\n\n            if(nums[mid] < nums[right]) {\n                right = mid;\n            } else {\n                left = mid + 1;\n            }\n        }\n\n        return nums[left];\n    }\n}\n```\n```python []\nclass Solution:\n    def findMin(self, nums: List[int]) -> int:\n        left, right = 0, len(nums) - 1\n        while left < right:\n            mid = left + (right - left) // 2\n            if nums[mid] < nums[right]:\n                right = mid\n            else:\n                left = mid + 1\n                \n        return nums[left]\n```\n```C []\nint findMin(int* nums, int numsSize) {\n    int left = 0, right = numsSize - 1;\n    while (left < right) {\n        int mid = left + (right - left) / 2;\n        if (nums[mid] < nums[right]) {\n            right = mid;\n        } else {\n            left = mid + 1;\n        }\n    }\n\n    return nums[left];\n}\n```\n```C# []\npublic class Solution {\n    public int FindMin(int[] nums) {\n        int left = 0, right = nums.Length - 1;\n        while (left <right) {\n            int mid = left + (right - left) / 2;\n            if (nums[mid] < nums[right]) {\n                right = mid;\n            } else {\n                left = mid + 1;\n            }\n        }\n        \n        return nums[left];\n    }\n}\n```\n```javascript []\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar findMin = function(nums) {\n    let left = 0, right = nums.length - 1;\n    while (left < right) {\n        let mid = Math.floor(left + (right - left) / 2);\n        \n        if (nums[mid] < nums[right]) {\n            right = mid;\n        } else {\n            left = mid + 1;\n        }\n    }\n    return nums[left];\n};\n```\n```Kotlin []\nclass Solution {\n    fun findMin(nums: IntArray): Int {\n        var left = 0\n        var right = nums.size - 1\n        while (left < right) {\n            val mid = left + (right - left) / 2\n            if (nums[mid] < nums[right]) {\n                right = mid\n            } else {\n                left = mid + 1\n            }\n        }\n        \n        return nums[left]\n    }\n}\n```\n\n## **"Ahoy there, navigator! We\'re diving into a sea of numbers, searching for the tiniest treasure in this digital jungle. But fear not, \'cause with this Super-duper binary search trick, we\'re slicing through those digits like a hot knife through butter! Who needs a compass when you\'ve got code? Let\'s navigate these waters and uncover that hidden gem, Super-style!"** \uD83C\uDFF4\u200D\u2620\uFE0F\n\n![franky.jpg](https://assets.leetcode.com/users/images/1c01d618-c062-4578-94fb-d1584436e1f6_1715552498.7268348.jpeg)\n
['Array', 'Binary Search', 'C', 'C++', 'Java', 'Python3', 'Kotlin', 'JavaScript', 'C#']