An element is called a peak element if its value is not smaller than the value of its adjacent elements(if they exists).
Given an array arr[] of size N, Return the index of any one of its peak elements.
Note: The generated output will always be 1 if the index that you return is correct. Otherwise output will be 0. 
Example 1:
Input: 
N = 3
arr[] = {1,2,3}
Possible Answer: 2
Generated Output: 1
Explanation: index 2 is 3.
It is the peak element as it is 
greater than its neighbour 2.
If 2 is returned then the generated output will be 1 else 0.
Example 2:
Input:
N = 3
arr[] = {3,4,2}
Possible Answer: 1
Output: 1
Explanation: 4 (at index 1) is the 
peak element as it is greater than 
it's neighbor elements 3 and 2.
If 1 is returned then the generated output will be 1 else 0.
 
Your Task:
You don't have to read input or print anything. Complete the function peakElement() which takes the array arr[] and its size N as input parameters and return the index of any one of its peak elements.
Can you solve the problem in expected time complexity?
 
Expected Time Complexity: O(log N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ A[] ≤ 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Complete search

Starter code: # your task is to complete this function

# function should return index to the any valid peak element

class Solution:   

    def peakElement(self,arr, n):

        # Code here



Solutions:
class Solution:

	def peakElement(self, arr, n):
		if len(arr) == 1:
			return 0
		if arr[0] >= arr[1]:
			return 0
		for i in range(1, n - 1):
			if arr[i] >= arr[i - 1] and arr[i] >= arr[i + 1]:
				return i
		if arr[n - 2] <= arr[n - 1]:
			return n - 1


class Solution:

	def peakElement(self, arr, n):
		i = 0
		if n == 1:
			return i
		for i in range(n):
			if i == 0 and arr[i] >= arr[i + 1]:
				return i
			if i == n - 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 peakElement(self, arr, n):
		low = 0
		high = n - 1
		while low < high:
			mid = (low + high) // 2
			if arr[mid] < arr[mid + 1]:
				low = mid + 1
			else:
				high = mid
		return low


class Solution:

	def peakElement(self, arr, n):
		return arr.index(max(arr))


class Solution:

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

	def peakElement(self, arr, n):
		return self.findPeakUtil(arr, 0, n - 1, n)


class Solution:

	def peakElement(self, nums, n):
		(l, r) = (0, len(nums) - 1)
		while l <= r:
			mid = (l + r) // 2
			if mid > 0 and nums[mid] < nums[mid - 1]:
				r = mid - 1
			elif mid < len(nums) - 1 and nums[mid] < nums[mid + 1]:
				l = mid + 1
			else:
				return mid


class Solution:

	def peakElement(self, arr, n):
		l = 0
		r = n - 1
		while l <= r:
			mid = l + r >> 1
			if (mid == 0 or arr[mid - 1] <= arr[mid]) and (mid == n - 1 or arr[mid + 1] <= arr[mid]):
				break
			if mid > 0 and arr[mid - 1] > arr[mid]:
				r = mid - 1
			else:
				l = mid + 1
		return mid


class Solution:

	def peakElement(self, arr, n):
		(lo, hi) = (0, n - 1)
		while lo <= hi:
			mid = (lo + hi) // 2
			if (mid == 0 or arr[mid - 1] <= arr[mid]) and (mid == n - 1 or arr[mid + 1] <= arr[mid]):
				return mid
			elif mid > 0 and arr[mid + 1] > arr[mid]:
				lo = mid + 1
			else:
				hi = mid - 1
		return lo + 1


class Solution:

	def peakElement(self, arr, n):
		if n > 1:
			for (i, elem) in enumerate(arr):
				if i == 0:
					prev = 0
					nxt = arr[i + 1]
				elif i == len(arr) - 1:
					prev = arr[i - 1]
					nxt = 0
				else:
					prev = arr[i - 1]
					nxt = arr[i + 1]
				if elem >= prev and elem >= nxt:
					return i
		else:
			return 0


class Solution:

	def peakElement(self, arr, n):
		max = 0
		for i in range(n):
			if arr[i] > arr[max]:
				max = i
		return max


class Solution:

	def peakElement(self, arr, n):
		for i in range(n):
			try:
				if arr[i] >= arr[i - 1] and arr[i] >= arr[i + 1]:
					return i
			except:
				return i


class Solution:

	def peakElement(self, arr, n):
		arr1 = sorted(arr)
		for i in range(n):
			if arr[i] == arr1[n - 1]:
				return i


class Solution:

	def peakElement(self, arr, n):
		ans = 0
		for i in range(0, n):
			if arr[i] > arr[i - 1]:
				ans = i
		return ans


class Solution:

	def peakElement(self, arr, n):
		result = arr[0]
		a = 0
		for i in range(len(arr)):
			if arr[i] > result:
				result = arr[i]
				a = i
		return a


class Solution:

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


class Solution:

	def peakElement(self, arr, n):
		l = 0
		h = n - 1
		while l <= h:
			mid = (l + h) // 2
			if (mid == 0 or arr[mid] >= arr[mid - 1]) and (mid == n - 1 or arr[mid] >= arr[mid + 1]):
				return mid
			elif mid > 0 and arr[mid - 1] > arr[mid]:
				h = mid - 1
			else:
				l = mid + 1


class Solution:

	def peakElement(self, arr, n):
		if n == 1:
			return 0
		elif n > 1:
			for (i, el) in enumerate(arr):
				if i == 0:
					if arr[0] >= arr[1]:
						return 0
				elif i == n - 1:
					if arr[n - 1] >= arr[n - 2]:
						return n - 1
				elif arr[i - 1] <= el >= arr[i + 1]:
					return i


class Solution:

	def peakElement(self, arr, n):
		ans = 0
		for i in range(n):
			if i != 0 and arr[i] >= arr[i - 1]:
				ans = i
			else:
				continue
		return ans


class Solution:

	def peakElement(self, arr, n):
		n = len(arr)
		(l, r) = (0, n - 1)
		while l < r:
			m = (l + r) // 2
			if arr[m + 1] > arr[m]:
				l = m + 1
			else:
				r = m
		return l


class Solution:

	def peakElement(self, arr, n):
		ans = max(arr)
		a = arr.index(ans)
		return a


class Solution:

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


class Solution:

	def peakElement(self, arr, n):
		a = max(arr)
		i = arr.index(a)
		return i


class Solution:

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


class Solution:

	def peakElement(self, arr, n):
		l = 0
		r = n - 1
		while l < r:
			middle = (r + l) // 2
			if arr[middle] > arr[middle + 1]:
				r = middle
			else:
				l = middle + 1
		return l


class Solution:

	def peakElement(self, arr, n):
		for (i, elem) in enumerate(arr):
			if i + 1 > len(arr) - 1:
				if arr[i] >= arr[i - 1]:
					return i
			if i - 1 >= 0 and i + 1 < len(arr) - 1:
				if arr[i] >= arr[i - 1] and arr[i] >= arr[i + 1]:
					return i
		return 0


class Solution:

	def peakElement(self, arr, n):
		if n == 1:
			return 0
		if arr[0] > arr[1]:
			return 0
		if arr[n - 1] > arr[n - 2]:
			return n - 1
		(l, r) = (0, n - 1)
		while l <= r:
			mid = (l + r) // 2
			if arr[mid - 1] <= arr[mid] and arr[mid + 1] <= arr[mid]:
				return mid
			elif arr[mid + 1] > arr[mid]:
				l = mid + 1
			elif arr[mid - 1] > arr[mid]:
				r = mid - 1


class Solution:

	def peakElement(self, arr, n):
		ans = float('-inf')
		index = -1
		for i in range(n):
			if arr[i] > ans:
				index = i
				ans = arr[i]
		return index


class Solution:

	def peakElement(self, arr, n):
		s = 0
		e = n - 1
		mid = s + (e - s) // 2
		while s < e:
			if arr[mid] < arr[mid + 1]:
				s = mid + 1
			else:
				e = mid
			mid = s + (e - s) // 2
		return s


class Solution:

	def peakElement(self, arr, n):
		l = 0
		r = n - 1
		while l < r:
			mid = l + r >> 1
			if arr[mid] < arr[mid + 1]:
				l = mid + 1
			else:
				r = mid
		return l


class Solution:

	def peakElement(self, arr, n):
		a = max(arr)
		b = arr.index(a)
		return b


class Solution:

	def peakElement(self, arr, n):
		if n == 1:
			return 0
		for (idx, num) in enumerate(arr):
			if idx == 0 and num > arr[idx + 1]:
				return 0
			elif idx == n - 1 and num > arr[idx - 1]:
				return n - 1
			if arr[idx - 1] <= arr[idx] and arr[idx + 1] <= arr[idx]:
				return idx