Big O Gym › Problem

Big O Practice: Find Min Rotated Sorted

Time and space complexity practice in Python and JavaScript. Concept tested: binary search on a comparison invariant.

def find_min(arr):
    lo, hi = 0, len(arr) - 1
    while lo < hi:
        mid = (lo + hi) // 2
        if arr[mid] > arr[hi]:
            lo = mid + 1
        else:
            hi = mid
    return arr[lo]

Variables

  • n - length of the rotated sorted array

Explanation

Binary-search variant comparing `mid` to `hi` to decide which half holds the rotation point - **O(log n)** time, **O(1)** space. The naive linear scan would be O(n).

More Big O practice problems · Today's daily problem