SIMPLE JAVASCRIPT✅EASY EXPLANATION✅TUTORIAL✅O(logn)TIME✅97.48%BEATS✅[47ms]
\n\n\n# How code works:\n\n          /-----------------------------\\\n          |                             |\nInput: [4, 5, 6, 7, 0, 1, 2, 3]\n        |    
![Screenshot 2023-04-08 at 01.33.37.png](https://assets.leetcode.com/users/images/4afa4390-9539-4cc8-bf7e-68948645cc30_1680910457.323517.png)\n\n\n# How code works:\n```\n          /-----------------------------\\\n          |                             |\nInput: [4, 5, 6, 7, 0, 1, 2, 3]\n        |                             |\n        \\-----------------------------/\n        \n      /-----------------------------\\\n      |                             |\nleft: 0                           right: 7\n      |                             |\n      \\-----------------------------/\n      \n         /---------------------\\\n         |                     |\nmid: 3                     nums[mid]: 7\n         |                     |\n         \\---------------------/\n         \nSince nums[mid] > nums[right], we move left pointer to mid+1\n       \n          /-----------------------------\\\n          |                             |\nInput: [4, 5, 6, 7, 0, 1, 2, 3]\n                      |              |\n                      \\--------------/\n        \n      /-----------------------------\\\n      |                             |\nleft: 4                           right: 7\n      |                             |\n      \\-----------------------------/\n      \n         /---------------------\\\n         |                     |\nmid: 5                    nums[mid]: 1\n         |                     |\n         \\---------------------/\n         \nSince nums[mid] < nums[right], we move right pointer to mid\n\n          /-----------------------------\\\n          |                             |\nInput: [4, 5, 6, 7, 0, 1, 2, 3]\n                      |        |\n                      \\--------/\n        \n      /-----------------------------\\\n      |                             |\nleft: 4                           right: 5\n      |                             |\n      \\-----------------------------/\n      \n         /---------------------\\\n         |                     |\nmid: 4                    nums[mid]: 0\n         |                     |\n         \\---------------------/\n         \nSince nums[mid] < nums[right], we move right pointer to mid\n\n          /-----------------------------\\\n          |                             |\nInput: [4, 5, 6, 7, 0, 1, 2, 3]\n                      |  |\n                      \\--/\n        \n      /-----------------------------\\\n      |                             |\nleft: 4                           right: 4\n      |                             |\n      \\-----------------------------/\n      \nSince left === right, we have found the minimum element: nums[left] (which is 0 in this case)\n\n```\n# Explanation\n1. It initializes two pointers left and right to the first and last indices of the array, respectively.\n\n2. The function enters a while loop that runs as long as left is less than right.\n\n3. Inside the while loop, the function calculates the middle index of the current range using the formula mid = Math.floor((left + right) / 2).\n\n4. The function checks whether the value at the middle index nums[mid] is greater than the value at the right index nums[right].\n\n5. If the value at the middle index is greater than the value at the right index, then the minimum element must be in the right half of the current range, so the left pointer is updated to mid + 1.\n\n6. If the value at the middle index is not greater than the value at the right index, then the minimum element must be in the left half of the current range, so the right pointer is updated to mid.\n\n7. The while loop continues until the range is reduced to a single element, at which point left and right point to the same index, and the minimum element is simply nums[left].\n\n8. Finally, the function returns the minimum element of the array.\n# Complexity\n- Time complexity:\nIt is O(log N), where N is the number of elements in the input array. This is because at each step of the binary search, the size of the search space is halved.\n\n- Space complexity:\nIt is O(1), which means that the amount of memory used by the algorithm is constant and does not depend on the size of the input array. This is because the algorithm only uses a few variables to keep track of the search space and does not create any additional data structures.\n\n# Code\n```\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar findMin = function (nums) {\n  let left = 0,\n    right = nums.length - 1;\n\n  while (left < right) {\n    let mid = Math.floor((left + right) / 2);\n    if (nums[mid] > nums[right]) left = mid + 1;\n    else right = mid;\n  }\n  return nums[left];\n};\n\n```
['JavaScript']