In the problem, we are given a sorted array. But this array is rotated at some point. Rotating an array implies that at a random point, the array was broken off into two subarrays and their positions were exchanged while rejoining the subarrays.
That means there would be a pivot element. This pivot element divides the array into two sorted subarrays. So one approach to the problem is to find the pivot point and compare the target with it, then check accordingly.
Or, do a regular binary search but at every iteration check if that half that is currently in focus is sorted or not: if it is sorted, well no worries. If it is not sorted, look into the other half.
Code:
Java
Big O Analysis
-
Runtime
The runtime complexity here is since we reduce the search sample space by half at each point so it’s exponentially faster at every step.
-
Memory
The memory usage is
O(1)
since we are not using any extra datastructures.
— A