C++/Java/Python/JavaScript || ✅🚀 Time: O(log n) & Space: O(1) Simple Code || ✔️🔥With Explanation
Intuition:\nThe code aims to find the minimum element in a rotated sorted array. It utilizes the binary search algorithm to efficiently narrow down the search s
# **Intuition:**\nThe code aims to find the minimum element in a rotated sorted array. It utilizes the binary search algorithm to efficiently narrow down the search space and locate the minimum element.\n\n# **Approach:**\n1. Initialize two pointers, `left` and `right`, representing the start and end indices of the search space.\n2. Enter a while loop as long as `left` is less than `right`.\n3. Calculate the midpoint `mid` using the formula `mid = left + (right - left) / 2`.\n4. Compare the value at index `mid` with the value at index `right` to determine which half of the array contains the minimum element.\n   - If `nums[mid] > nums[right]`, it means the minimum element lies in the right half of the array. Update `left = mid + 1` to search in the right half.\n   - Otherwise, the minimum element lies in the left half of the array, including the `mid` index. Update `right = mid` to search in the left half.\n5. Repeat steps 3-4 until the search space is narrowed down to a single element, i.e., `left >= right`.\n6. Return the value at `nums[left]` as the minimum element.\n\n# **Complexity:**\n- Time Complexity: The binary search approach has a time complexity of O(log n), where n is the size of the input array `nums`. The search space is halved in each iteration, leading to efficient searching.\n- Space Complexity: The code has a space complexity of O(1) as it uses a constant amount of extra space for the variables `left`, `right`, and `mid`. No additional data structures are used that scale with the input size.\n\n# C++\n```\nclass Solution {\npublic:\n    int findMin(vector<int>& nums) {\n        int left = 0;\n        int right = nums.size() - 1;\n\n        while (left < right) {\n            int mid = left + (right - left) / 2;\n\n            if (nums[mid] > nums[right]) {\n                left = mid + 1;\n            } else {\n                right = mid;\n            }\n        }\n\n        return nums[left];\n    }\n};\n\n```\n# Java\n```\nclass Solution {\n    public int findMin(int[] nums) {\n        int left = 0;\n        int right = nums.length - 1;\n\n        while (left < right) {\n            int mid = left + (right - left) / 2;\n\n            if (nums[mid] > nums[right]) {\n                left = mid + 1;\n            } else {\n                right = mid;\n            }\n        }\n\n        return nums[left];\n    }\n}\n```\n# Python\n```\nclass Solution(object):\n    def findMin(self, nums):\n        left = 0\n        right = len(nums) - 1\n\n        while left < right:\n            mid = left + (right - left) / 2\n\n            if nums[mid] > nums[right]:\n                left = mid + 1\n            else:\n                right = mid\n\n        return nums[left]\n\n\n```\n# JavaScript\n```\nvar findMin = function(nums) {\n    let left = 0;\n    let right = nums.length - 1;\n\n    while (left < right) {\n        let mid = left + Math.floor((right - left) / 2);\n\n        if (nums[mid] > nums[right]) {\n            left = mid + 1;\n        } else {\n            right = mid;\n        }\n    }\n\n    return nums[left];\n};\n\n```
['Python', 'C++', 'Java', 'JavaScript']