[JavaScript] Using Binary Search (easy to understand)
Before we go forward, this is what we know:\n- The array is rotated, and it could be rotated in a way it does not look rotated at all (smallest value on left, l
Before we go forward, this is what we know:\n- The array is rotated, and it could be rotated in a way it does not look rotated at all (smallest value on left, largest value on right)\n- If the array is shifted, there will be ONE occurance where\n\n`a[mid - 1] > a[mid]`\n\nYou see this in `[4,5,1,2,3]`. There\'s only one time where the mid - 1 value is greater than mid value. That\'s a key to finding the solution.\n\nI wrote all of the if/else if statements with these tests. It\'s all you need! \n\n[1,2,3,4,5]\n[5,1,2,3,4]\n[4,5,1,2,3]\n[3,4,5,1,2]\n[2,3,4,5,1]\n\nThis helps us with edge cases like "what if the array is regularly sorted" or "what if the values is all the way on the right? The `[5,1,2,3,4]` needs a special case just for itself (a[mid] < a[left] && a[mid] < a[right]).\n\n**Space Complexity**: O(1)\n**Time Complexity** : O(log n)\n\n```\nconst findMin = (a) => {\n\tif (!a || a.length === 0) return null;\n\n\tlet left = 0,\n\t\tright = a.length - 1;\n\n\twhile (left < right) {\n\t\tlet mid = (left + (right - left) / 2) | 0; // This helps avoid overflows with large data sets\n\t\t\n\t\t// The one instance I mentioned above\n\t\tif (mid > 0 && a[mid - 1] > a[mid]) return a[mid];\n\t\telse if (a[mid] >= a[left] && a[mid] < a[right]) right = mid - 1;\n\t\telse if (a[mid] >= a[left] && a[mid] > a[right]) left = mid + 1;\n\t\telse if (a[mid] < a[left] && a[mid] < a[right]) right = mid - 1;\n\t}\n\n\treturn a[left];\n};\n```
['Binary Search', 'JavaScript']