def upper_bound(arr, value):
    """
    find the index of the last element in arr <= value.
    """
    low = 0
    high = len(arr) - 1
    found = -1
    while(low <= high):
        mid = (low + high) // 2
        if arr[mid] > value:
            high = mid - 1
        else:
            found = mid
            low = mid + 1
    return found