# Uses a binary search algorithm to find the index of the maximum element in
# a bitonic sequence

def findBitonicMax(L, first, last):
    # Base case: list L[first:last+1] has exactly one element
    if first == last:
        return first

    # Base case: list L[first:last+1] has exactly two elements
    if first + 1 == last:
        if L[first] < L[last]:
            return last
        else:
            return first
        
    # Recursive case: last > first + 1; list L[first:last+1] has more than 2 elements
    mid = (first + last)/2
    
    # Element at mid is the largest
    if L[mid] > L[mid-1] and L[mid] > L[mid+1]:
        return mid
    
    # mid is in ascending part of the sequence
    if L[mid-1] < L[mid] and L[mid] < L[mid + 1]:
        return findBitonicMax(L, mid+1, last)
    
    # mid is in descending part of the sequence
    if L[mid-1] > L[mid] and L[mid] > L[mid + 1]:
        return findBitonicMax(L, first, mid-1)
