def smallest_missing(A, left_element, right_element):
    if not A:
        return 0

    n = len(A)
    left = max(0, left_element)
    right = min(right_element, n - 1)

    if left > right:
        return 0

    base = A[left] - left

    if A[left] != left + base:
        return left + base

    if A[right] == right + base:
        return right + 1 + base

    while left <= right:
        mid = (left + right) // 2
        expected = mid + base
        if A[mid] == expected:
            left = mid + 1
        else:
            right = mid - 1

    return left + base