Let's call an array arr a mountain if the following properties hold:

arr.length >= 3
There exists some i with 0 < i < arr.length - 1 such that:
        
arr[0] < arr[1] < ... arr[i-1] < arr[i] 
arr[i] > arr[i+1] > ... > arr[arr.length - 1]



Given an integer array arr that is guaranteed to be a mountain, return any i such that arr[0] < arr[1] < ... arr[i - 1] < arr[i] > arr[i + 1] > ... > arr[arr.length - 1].
 
Example 1:
Input: arr = [0,1,0]
Output: 1
Example 2:
Input: arr = [0,2,1,0]
Output: 1
Example 3:
Input: arr = [0,10,5,2]
Output: 1
Example 4:
Input: arr = [3,4,5,1]
Output: 2
Example 5:
Input: arr = [24,69,100,99,79,78,67,36,26,19]
Output: 2

 
Constraints:

3 <= arr.length <= 104
0 <= arr[i] <= 106
arr is guaranteed to be a mountain array.

Difficulty: introductory

Starter code:

class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        

Solutions:
class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        l,r = 0,len(arr)-1
        while l<=r:
            m = (l+r)//2
            if (arr[m]>=arr[m-1])and (arr[m]>=arr[m+1]):
                return m
            else:
                if arr[m-1]>arr[m]:
                    r = m-1
                else:
                    l = m+1


class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        hi = len(arr) - 1
        lo = 0
        while lo < hi:
            mid = (lo + hi) // 2
            if arr[mid - 1] < arr[mid] > arr[mid + 1]:
                return mid
            elif arr[mid - 1] >= arr[mid]:
                hi = mid
            else:
                lo = mid


class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        left, right = 0, len(arr) - 1
        while left < right:
            mid = (left + right) // 2
            if arr[mid] < arr[mid + 1]:
                left = mid + 1
            else:
                right = mid

        return left


class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        for i in range(1,len(arr)-1):
            if arr[i-1] < arr[i] > arr[i+1]:
                return i
            
        
            


class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        l = 0
        r = len(arr) - 1
        while l < r:
            if arr[r-1] >= arr[r]:
                r -= 1
            if arr[l + 1] >= arr[l]:
                l += 1
        return r

class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        left, right = 0, len(arr) - 1
        while left < right:
            mid = (left + right) // 2
            if (mid == 0 or arr[mid - 1] < arr[mid]) and arr[mid] < arr[mid + 1]:
                left = mid + 1
            elif (mid == 0 or arr[mid - 1] > arr[mid]) and arr[mid] > arr[mid + 1]:
                right = mid - 1
            else:
                return mid

        return left


class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        
        for i in range(len(arr)):
            if i == 0 and arr[i] > arr[i + 1]:
                return i
            elif i == len(arr) - 1 and arr[i] > arr[i - 1]:
                return i
            elif arr[i] > arr[i + 1] and arr[i] > arr[i - 1]:
                return i

class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        #return arr.index(max(arr))
        
        left, right = 0, len(arr)-1
        
        while left <= right:
            mid = (left+right)//2
            if arr[left] < arr[left+1]:
                left+= 1
            if arr[right] < arr[right-1]:
                right-= 1
            if arr[mid-1] < arr[mid] and arr[mid] > arr[mid+1]:
                return mid
        
        return mid


class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        l, r = 0, len(arr)-1
        
        while l<=r:
            m = l+(r-l)//2
            
            if arr[m-1] < arr[m] > arr[m+1]:
                return m
            elif arr[m-1] < arr[m] < arr[m+1]:
                l = m
            else:
                r = m


class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        l, r = 0, len(arr) - 1
        while l <= r:
            m = l + (r-l) // 2
            if m + 1 < len(arr) and arr[m] < arr[m+1]:
                l = m+1
            else:
                r = m-1
        return l

class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        l, r = 0, len(arr) - 1
        
        while l < r:
            if arr[l] > arr[l+1]:
                return l
            if arr[r-1] < arr[r]:
                return r
            l, r = l + 1, r - 1
        return l

class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        # # linear scan
        # i = 0
        # while arr[i + 1] > arr[i]:
        #     i += 1
        # return i
    
        # binary search
        left = 0
        right = len(arr) - 1
        while True:
            mid = (left + right)//2
            if (arr[mid] > arr[mid - 1]) and (arr[mid + 1] > arr[mid]):
                left = mid
            elif (arr[mid] < arr[mid - 1]) and (arr[mid + 1] < arr[mid]):
                right = mid
            else:
                return mid


class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        i, j = 0, len(arr) - 1
        
        peak = 1
        
        while i < j:
            mid = (i + j) // 2
            if arr[mid] < arr[mid + 1]:
                i = mid + 1
            else:
                j = mid
                
        return i
            


class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        l = 0
        # r = len(arr) - 1
        while arr[l] < arr[l + 1]:
            l += 1
        return l
        # while l < r:
        #     mid = (l + r)//2
        #     if arr[mid] < arr[mid + 1]:
        #         l = mid + 1
        #     else:
        #         r = mid
        # return r
        # while l < r:
        #     if arr[r-1] >= arr[r]:
        #         r -= 1
        #     if arr[l + 1] >= arr[l]:
        #         l += 1
        # return r


# O(logn), binary search for peak since arr is ordered
class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        l = 0
        r = len(arr)
        while l < r:
            m = (l + r) // 2
            if arr[m-1] < arr[m] > arr[m+1]:
                return m
            if arr[m] < arr[m+1]: # peak on right
                l = m + 1
            else: # peak on left
                r = m
        return l

'''
# O(n), distinct elements so get max and return it's index
class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        return arr.index(max(arr))
'''

class Solution(object):
    def peakIndexInMountainArray(self, A):
        lo, hi = 0, len(A) - 1
        while lo < hi:
            mi = (lo + hi) // 2
            if A[mi] < A[mi + 1]:
                lo = mi + 1
            else:
                hi = mi
        return lo

class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        l,r=0,len(arr)-1
        while(l<=r):
            mid = l + (r - l) // 2; 
            print(mid)
            if(arr[mid-1]<arr[mid]>arr[mid+1]):
                return mid
            elif(arr[mid-1]>arr[mid]):
                r=r-1
            else:
                l=l+1


class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        i = 0
        size = len(arr)
        # increasing check
        while i < size - 1 and arr[i] < arr[i + 1]:
            i += 1
        return i

class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        l = 0 
        r = len(arr) - 1
        
        while l <= r:
            mid = (l + r) // 2
            if mid > 0 and arr[mid-1] < arr[mid] > arr[mid+1]:
                return mid
            
            if arr[mid] < arr[mid+1]:
                # increasing
                l = mid + 1
            else:
                # decreasing
                r = mid - 1

class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        
        s = 0
        e = len(arr) - 1
        
        while s < e:
            m = (e + s) // 2
            if (arr[m] < arr[m + 1]):
                s = m + 1
            else:
                e = m
                
        return s


class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        low = 0
        high = len(arr) - 1
        
        while low < high:
            mid1 = (low + high)//2
            mid2 = mid1 + 1
            
            if arr[mid1] <= arr[mid2]:
                low = mid2
            else:
                high = mid1
        return low

class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        i, j = 0, len(arr)-1
        while i<j:
            m = (i+j)//2
            if arr[m] > arr[m+1]:
                j = m
            else:
                i = m+1
        return i

class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        
        for i in range(0, len(arr) - 1):
            if arr[i + 1] < arr[i]:
                return i
            
            


class Solution:
    def peakIndexInMountainArray(self, arr: List[int]) -> int:
        
        for i in range(1,len(arr)-1):
            if arr[i] > arr[i+1] and arr[i] > arr[i-1]:
                return i