Search for a command to run...
Try your own input
Change the array and replay the algorithm from step one.
Current step
A rotated array is two sorted runs joined end to end. Any midpoint splits it so that at least one side is fully sorted — find which, and a normal range check decides whether 1 can live there.
Modified Binary Search
1function search(nums, target) {2 let low = 0, high = nums.length - 1;//target: 1low: 0high: 83 4 while (low <= high) {5 const mid = Math.floor((low + high) / 2);6 7 if (nums[mid] === target) return mid;8 9 // Left half is sorted10 if (nums[low] <= nums[mid]) {11 if (nums[low] <= target && target <= nums[mid]) {12 high = mid - 1; // target lies in it13 } else {14 low = mid + 1; // it cannot15 }16 }17 // Otherwise the right half is sorted18 else {19 if (nums[mid] <= target && target <= nums[high]) {20 low = mid + 1;21 } else {22 high = mid - 1;23 }24 }25 }26 return -1;27}