Given an array in such a way that the elements stored in array are in increasing order initially and then after reaching to a peak element, the elements stored are in decreasing order. Find the highest element.
Note: A[i] != A[i+1]
 
Example 1:
Input:
11
1 2 3 4 5 6 5 4 3 2 1
Output: 6
Explanation: Highest element is 6.
Example 2:
Input:
5
1 2 3 4 5
Output: 5
Explanation: Highest element is 5.
 
Your Task:
You don't need to read or print anything. Your task is to complete the function findPeakElement() which takes the array as the input parameter and returns the highest element.
 
Expected Time Complexity: O(log(n))
Expected Space Complexity: O(1)
 
Constraints:
2 <= Size of array <= 200
1 <= Elements of the array <= 1000000

Source: geeksforgeeks | Difficulty: EASY
Tags: Sorting

Starter code: #User function Template for python3



class Solution:

	def findPeakElement(self, a):

		# Code here



Solutions:
class Solution:

	def findPeakElement(self, a):
		first = 0
		last = len(a) - 1
		while first <= last:
			mid = (first + last) // 2
			if first == last or (a[mid] > a[mid + 1] and a[mid] > a[mid - 1]):
				return a[mid]
			elif a[mid] >= a[mid - 1] and a[mid] <= a[mid + 1]:
				first = mid + 1
			elif a[mid] >= a[mid + 1] and a[mid] <= a[mid - 1]:
				last = mid - 1


class Solution:

	def findPeakElement(self, a):
		return max(a)


class Solution:

	def findPeakElement(self, a):
		x = a[0]
		for i in range(1, len(a)):
			if a[i] > a[i - 1]:
				x = a[i]
		return x


class Solution:

	def findPeakElement(self, a):
		a.sort()
		peak = a[-1]
		return peak


class Solution:

	def findPeakElement(self, a):
		a.sort(reverse=True)
		return a[0]


class Solution:

	def findPeakElement(self, arr):
		(low, high) = (0, len(arr) - 1)
		while low <= high:
			mid = low + (high - low) // 2
			if (mid == len(arr) - 1 or mid == 0) or (arr[mid - 1] < arr[mid] and arr[mid + 1] < arr[mid]):
				return arr[mid]
			elif arr[mid - 1] <= arr[mid] and arr[mid + 1] >= arr[mid]:
				low = mid + 1
			else:
				high = mid - 1
		return -1


class Solution:

	def findPeakElement(self, a):
		for i in range(len(a)):
			if len(a) == 1000000:
				return 200
			elif a[i] > a[i + 1]:
				return a[i]


class Solution:

	def findPeakElement(self, nums):
		n = len(nums)
		(left, right) = (0, n - 1)
		while left < right:
			mid = (left + right) // 2
			if nums[mid] > nums[mid + 1]:
				right = mid
			else:
				left = mid + 1
		return nums[left]


class Solution:

	def findPeakElement(self, arr):
		start = 0
		end = n - 1
		while start <= end:
			mid = int((start + end) / 2)
			if mid == n - 1:
				return arr[mid]
			if arr[mid] > arr[mid - 1] and arr[mid] > arr[mid + 1]:
				return arr[mid]
			elif arr[mid] > arr[mid - 1]:
				start = mid + 1
			else:
				end = mid - 1
		return -1


def bs(arr):
	(st, en) = (0, len(arr) - 1)
	while st <= en:
		mid = (st + en) // 2
		if (mid < len(arr) - 1 and arr[mid] > arr[mid + 1]) and (mid > 0 and arr[mid - 1] < arr[mid]):
			return arr[mid]
		if mid < len(arr) - 1 and mid > 0 and (arr[mid] > arr[mid + 1]) and (arr[mid - 1] > arr[mid]):
			en = mid - 1
		else:
			st = mid + 1
	return arr[-1]

class Solution:

	def findPeakElement(self, a):
		return bs(a)


class Solution:

	def findPeakElement(self, a):
		m = sorted(a)
		n = len(m)
		return m[n - 1]


class Solution:

	def findPeakElement(self, arr):
		low = 0
		high = len(a) - 1
		while low <= high:
			mid = (high + low) // 2
			if arr[mid] > arr[mid - 1]:
				low = mid + 1
			else:
				high = mid - 1
		return arr[high]


class Solution:

	def findPeakElement(self, a):
		(l, h) = (0, len(a) - 1)
		while l < h:
			m = (l + h) // 2
			if a[m] < a[m + 1]:
				l = m + 1
			else:
				h = m
		return a[h]


class Solution:

	def findPeakElement(self, a):
		highest = a[0]
		for i in range(len(a)):
			if a[i] > highest:
				highest = a[i]
		return highest


class Solution:

	def findPeakElement(self, a):
		n = len(a)
		jax = a.sort()
		return a[n - 1]


class Solution:

	def findPeakElement(self, a):
		curr = 0
		for i in a:
			if i > curr:
				curr = i
		return curr


class Solution:

	def findPeakElement(self, a):
		l = 0
		h = len(a) - 1
		if len(a) == 1:
			return a[0]
		if len(a) == 2:
			if a[1] > a[0]:
				return a[1]
			return a[0]
		while l <= h or mid < len(a) - 2:
			mid = (l + h) // 2
			if mid == len(a) - 1 and a[mid] > a[mid - 1]:
				return a[mid]
			if a[mid] > a[mid - 1] and a[mid] > a[mid + 1]:
				return a[mid]
			if a[mid] < a[mid + 1]:
				l = mid + 1
			elif a[mid] > a[mid + 1]:
				h = mid - 1
			elif a[mid] < a[mid - 1]:
				h = mid - 1
			elif a[mid] > a[mid - 1]:
				l = mid + 1


class Solution:

	def findPeakElement(self, a):
		s = 0
		e = len(a) - 1
		if a[e] > a[e - 1]:
			return a[e]
		while s <= e:
			m = (s + e) // 2
			if a[m] > a[m - 1] and a[m] > a[m + 1]:
				return a[m]
			elif a[m] > a[m - 1] and a[m] < a[m + 1]:
				s = m
			elif a[m] < a[m - 1] and a[m] > a[m + 1]:
				e = m


class Solution:

	def findPeakElement(self, a):
		a.sort()
		l = len(a)
		return a[l - 1]


class Solution:

	def findPeakElement(self, a):
		start = 0
		end = len(a) - 1
		n = len(a)
		while start <= end:
			mid = start + (end - start) // 2
			prev = (mid - 1) % n
			next = (mid + 1 - n) % n
			if a[prev] < a[mid] and a[next] < a[mid]:
				return a[mid]
			elif a[mid] < a[next]:
				start = mid + 1
			else:
				end = mid - 1
		return -1


class Solution:

	def findPeakElement(self, a):
		left = 0
		right = len(a) - 1
		while left < right:
			if a[left] >= a[right]:
				right -= 1
			else:
				left += 1
		return a[left]


class Solution:

	def findPeakElement(self, a):
		left = 0
		right = len(a) - 1
		while left <= right:
			mid = (left + right) // 2
			if mid == 0 and a[mid + 1] > a[mid]:
				return a[mid + 1]
			elif mid == len(a) - 1 and a[mid - 1] > a[mid]:
				return a[mid - 1]
			if mid == 0 or mid == len(a) - 1 or (a[mid] > a[mid - 1] and a[mid] > a[mid + 1]):
				return a[mid]
			elif a[mid] > a[mid - 1] and a[mid] < a[mid + 1]:
				left = mid + 1
			else:
				right = mid - 1
		return a[(left + right) // 2]


class Solution:

	def findPeakElement(self, a):
		(s, e) = (0, len(a) - 1)
		while s < e:
			mid = s + (e - s) // 2
			if a[mid] > a[mid + 1]:
				e = mid
			else:
				s = mid + 1
		return a[s]


class Solution:

	def findPeakElement(self, a):
		if a[0] > a[1]:
			return a[0]
		if a[-1] > a[-2]:
			return a[-1]
		low = 0
		high = len(a) - 1
		while True:
			i = (low + high) // 2
			if a[i - 1] < a[i] > a[i + 1]:
				return a[i]
			if a[i + 1] > a[i]:
				low = i + 1
			else:
				high = i - 1


class Solution:

	def findPeakElement(self, a):
		i = 0
		while i < len(a) - 1:
			if a[i] > a[i + 1]:
				return a[i]
			i = i + 1
		return a[-1]


class Solution:

	def findPeakElement(self, a):
		l = 0
		h = len(a) - 1
		ma = a[0]
		while l <= h:
			mid = (l + h) // 2
			if ma < a[mid]:
				ma = a[mid]
				l = mid + 1
			elif ma > a[mid]:
				h = mid - 1
			else:
				l = mid + 1
				h = mid - 1
		return ma


class Solution:

	def findPeakElement(self, a):
		maxElem = a[0]
		(l, r) = (0, len(a) - 1)
		while l <= r:
			mid = (l + r) // 2
			maxElem = max(maxElem, a[mid])
			if a[l] <= a[mid]:
				l = mid + 1
			else:
				r = mid - 1
		return maxElem


class Solution:

	def findPeakElement(self, a):
		(low, high) = (0, len(a) - 1)
		mid = 0
		while low <= high:
			mid = low + high >> 1
			if mid == 0 or mid == len(a) - 1 or (a[mid + 1] < a[mid] and a[mid - 1] < a[mid]):
				break
			elif a[mid + 1] > a[mid]:
				low = mid + 1
			else:
				high = mid - 1
		return a[mid]


class Solution:

	def findPeakElement(self, a):
		ans = a[0]
		i = 1
		while i < len(a) and a[i - 1] < a[i]:
			ans = a[i]
			i += 1
		return ans


class Solution:

	def findPeakElement(self, a):
		if len(a) == 1:
			return a[0]
		for i in range(2, len(a) - 1):
			j = i - 1
			if a[j] < a[i] and a[i + 1] < a[i]:
				return a[i]
		return a[len(a) - 1]


class Solution:

	def findPeakElement(self, a):
		a = sorted(a)
		return a[-1]


class Solution:

	def findPeakElement(self, a):
		if len(a) <= 1:
			return a[0]
		start = 0
		end = len(a) - 1
		mid = start + (end - start) // 2
		if a[mid] > a[mid + 1]:
			return self.findPeakElement(a[:mid + 1])
		else:
			return self.findPeakElement(a[mid + 1:])
		return -1


class Solution:

	def findPeakElement(self, a):
		s = 0
		e = len(a) - 1
		m = s + (e - s) // 2
		while s < e:
			if a[m] < a[m + 1]:
				s = m + 1
				ans = a[s]
			else:
				e = m
			m = s + (e - s) // 2
		return ans


class Solution:

	def findPeakElement(self, a):
		begin = 0
		end = len(a) - 1
		while end - begin > 1:
			m = (begin + end) // 2
			if a[m] > a[m - 1] and a[m] > a[m + 1]:
				return a[m]
			elif a[m] < a[m + 1] and a[m] > a[m - 1]:
				begin = m + 1
			elif a[m] > a[m + 1] and a[m] < a[m - 1]:
				end = m - 1
		if a[begin] > a[end]:
			return a[begin]
		else:
			return a[end]


class Solution:

	def findPeakElement(self, a):
		return peakHelper(a, 0, len(a) - 1)

def peakHelper(arr, left, right):
	if right <= left + 1:
		return max(arr[right], arr[left])
	mid = (left + right) // 2
	if 1 < mid < len(arr) - 1 and (arr[mid] > arr[mid - 1] and arr[mid] > arr[mid + 1]):
		return arr[mid]
	elif arr[mid] < arr[mid - 1]:
		return peakHelper(arr, left, mid - 1)
	else:
		return peakHelper(arr, mid + 1, right)


class Solution:

	def binarysearch(self, arr, l, r):
		if l <= r:
			mid = (l + r) // 2
			if arr[mid] > arr[mid + 1]:
				return self.binarysearch(arr, l, mid - 1)
			else:
				return self.binarysearch(arr, mid + 1, r)
		return l

	def findPeakElement(self, a):
		if len(a) == 1:
			return a[0]
		elif a[-1] > a[-2]:
			return a[-1]
		return a[self.binarysearch(a, 0, len(a) - 1)]


class Solution:

	def findPeakElement(self, a):
		l = 0
		h = n - 1
		while l < h:
			mid = (l + h) // 2
			if a[mid] < a[mid + 1]:
				l = mid + 1
			else:
				h = mid
		return a[h]


class Solution:

	def findPeakElement(self, a):
		a.sort()
		a = a[::-1]
		return a[0]


class Solution:

	def findPeakElement(self, a):
		k = set(a)
		return max(k)


class Solution:

	def findPeakElement(self, a):
		myset = set(a)
		return max(myset)


def findpeak(a):
	n = len(a)
	start = 0
	end = n - 1
	while start <= end:
		mid = (start + end) // 2
		prev = (mid + n - 1) % n
		nexts = (mid + 1) % n
		if a[mid] > a[nexts] and a[mid] > a[prev]:
			return a[mid]
		elif a[mid] > a[nexts]:
			end = mid - 1
		else:
			start = mid + 1

class Solution:

	def findPeakElement(self, a):
		res = findpeak(a)
		return res


class Solution:

	def findPeakElement(self, a):
		for i in range(1, len(a) - 1):
			if a[i - 1] < a[i] > a[i + 1]:
				return a[i]
		if a[-2] < a[-1]:
			return a[-1]


class Solution:

	def findPeakElement(self, nums):
		i = 0
		j = len(nums) - 1
		while i <= j:
			mid = (i + j) // 2
			if (mid == 0 or nums[mid - 1] <= nums[mid]) and (mid == len(nums) - 1 or nums[mid] >= nums[mid + 1]):
				return nums[mid]
			if mid > 0 and nums[mid - 1] > nums[mid]:
				j = mid - 1
			else:
				i = mid + 1
		return -1


class Solution:

	def findPeakElement(self, a):
		(l, r) = (0, len(a) - 1)
		while l <= r:
			mid = (l + r) // 2
			if mid == n - 1 or a[mid - 1] < a[mid] > a[mid + 1]:
				return a[mid]
			elif a[mid - 1] < a[mid] < a[mid + 1]:
				l = mid + 1
			elif a[mid - 1] > a[mid] > a[mid + 1]:
				r = mid - 1