1. Search in Rotated Sorted Array

Let's formalize the problem: we need to find a number in array A[n]:

  • A[0 .. i] is strictly increasing
  • A[i+1 .. n-1] is strictly increasing
  • A[0] > A[n-1]

Utilising the above special properties of A[] we can use binary search as follow:

  • Case 1: A[mid] == target => result is mid obviously, return.
  • Case 2: mid ∈ [left,i] <=> A[mid] >= A[0]
- if (A[mid] > target >= A[left]) right = mid - 1;
- else left = mid + 1;
  • Case 3: mid ∈ (i,right] <=> A[mid] < A[0]
- if (A[mid] < target <= A[right]) left = mid + 1;
- else right = mid - 1;

=> Runtime O(logn)

    int search(vector<int>& nums, int target) {
        int n = nums.size();
        if (n == 0) return -1;
        int left = 0, right = n - 1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) return mid;
            if (nums[mid] >= nums[0]) {
                if (nums[mid] > target && target >= nums[left]) 
                    right = mid - 1;
                else 
                    left = mid + 1;
            } else {
                if (nums[mid] < target && target <= nums[right]) 
                    left = mid + 1;
                else
                    right = mid - 1;
            }
        }
        return -1;
    }
2. Find Minimum in Rotated Sorted Array

Similar to above problem, instead of finding a target number, we find the rotated point.

int findMin(vector<int>& nums) {
        int left = 0, right = nums.size() - 1;
        int rotatedPoint = -1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (mid + 1 < nums.size() && nums[mid] > nums[mid+1]) {
                rotatedPoint = mid;
                break;
            }
            if (nums[mid] >= nums[left])
                left = mid + 1;
            else 
                right = mid - 1;
        }
        return nums[rotatedPoint + 1];
}
3. Search in Rotated Sorted Array II

Let's formalize the problem: we need to find a number in array A[n]:

  • A[0 .. i] is increasing
  • A[i+1 .. n-1] is increasing
  • A[0] >= A[n-1]
  • Case 1: a[mid] == target => result is mid obviously, return.
  • Case 2: mid ∈ (j, i] <=> a[mid] > a[0]
- if A[left] <= target < A[mid] right = mid - 1
- else left = mid + 1
  • Case 3: mid ∈ (i, k) <=> A[mid] < A[n-1]
- if (A[mid] < target <= A[right] left = mid + 1
- else right = mid - 1;
  • Case 4: A[left] == A[mid] or A[right] == A[mid] => we don't know which part mid belong to => we cannot reduce the search space by half as previous cases. We have to reduce the search space by 1: either from left or right end.

Suppose that I choose left side: first we need to check if A[left] == target, if yes => return left, otherwise left++.

    bool search(vector<int>& nums, int target) {
        int left = 0, right = nums.size() - 1;
        int n = right;
        if (right < 0) return false;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (nums[mid] == target) return true;
            if (nums[mid] > nums[0]) {
                if (target < nums[mid] && target >= nums[left])
                    right = mid - 1;
                else 
                    left = mid + 1;
            } else if (nums[mid] < nums[n]) {
                if (target > nums[mid] && target <= nums[right])
                    left = mid + 1;
                else 
                    right = mid - 1;
            } else {
                if (nums[left] == target) return true;
                left++;
            }
        }
        return false;
    }

Runtime: O(n) in worst case (all elements are equal, we reduce the search space by 1 each time)

4. Find Minimum in Rotated Sorted Array II

Similar to problem 3, this time we find the rotated point

    int findMin(vector<int>& nums) {
        int left = 0, right = nums.size() - 1;
        int n = nums.size();
        int res = -1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if (mid + 1 < nums.size() && nums[mid] > nums[mid+1]) {
                res = mid;
                break;
            }
            
            if (nums[mid] > nums[0]) {
                left = mid + 1;
            } else if (nums[mid] < nums[n-1]) {
                right = mid - 1;
            } else {
                if (left + 1 < nums.size() && nums[left] > nums[left+1]) {
                    res = left;
                    break;
                }
                left++;
            }
               
        }
        return nums[res+1];
    }

Runtime: O(n) in worst case (all elements are equal, we reduce the search space by 1 each time)