This problem is similar to leetcode 33. We want the minimum element in a rotated sorted array, this rotation could have happened n times. Basically, we want to find the pivot or rotation point.
Well, we can use binary search and at every iteration check if the is greater than , if yes then reduce the search space to right side subarray. If no, the reduce search space to the left side subarray.
It’s important to understand that implies that the pivot element/rotation point/smallest element lies somewhere in . Similarly, if this isn’t the case, then the smallest element lies somewhere in subarray.
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