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.

Source: leetcode | Difficulty: EASY
Tags: Sorting

Data structures

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
			elif 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:
		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:
		(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
		while arr[l] < arr[l + 1]:
			l += 1
		return l


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)
		while i < size - 1 and arr[i] < arr[i + 1]:
			i += 1
		return i


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