Given an array arr of n elements that is first strictly increasing and then maybe strictly decreasing, find the maximum element in the array.
Note: If the array is increasing then just print the last element will be the maximum value.
Example 1:
Input: 
n = 9
arr[] = {1,15,25,45,42,21,17,12,11}
Output: 45
Explanation: Maximum element is 45.
Example 2:
Input: 
n = 5
arr[] = {1, 45, 47, 50, 5}
Output: 50
Explanation: Maximum element is 50.
Your Task:  
You don't need to read input or print anything. Your task is to complete the function findMaximum() which takes the array arr[], and n as parameters and returns an integer denoting the answer.
Expected Time Complexity: O(logn)
Expected Auxiliary Space: O(1)
Constraints:
3 ≤ n ≤ 10^{6}
1 ≤ arr_{i} ≤ 10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: Data structures

Complete search

Starter code: #User function Template for python3

class Solution:



	def findMaximum(self,arr, n):

		# code here

Solutions:
class Solution:

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


class Solution:

	def findMaximum(self, arr, n):
		return max(arr)


class Solution:

	def findMaximum(self, arr, n):
		arr.sort()
		c = max(arr)
		return c


class Solution:

	def findMaximum(self, arr, n):
		i = 1
		while i < n - 1:
			if arr[i] > arr[i - 1] and arr[i] > arr[i + 1]:
				return arr[i]
			i += 1
		return arr[n - 1]


class Solution:

	def findMaximum(self, arr, n):
		left = 0
		right = n - 1
		while left < right:
			mid = (right + left) // 2
			curr = arr[mid]
			if arr[mid] < arr[mid + 1]:
				left = mid + 1
			else:
				right = mid
		return arr[left]


class Solution:

	def findMaximum(self, arr, n):
		arr.sort()
		return arr[n - 1]


class Solution:

	def findMaximum(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 arr[end]


class Solution:

	def findMaximum(self, arr, n):
		for i in range(1, n):
			if arr[i] < arr[i - 1]:
				return arr[i - 1]
		return arr[len(arr) - 1]


class Solution:

	def findMaximum(self, arr, n):
		b = -1
		for i in range(n - 1):
			if arr[i] > arr[i + 1]:
				b = i
				break
		return arr[b]


class Solution:

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


class Solution:

	def findMaximum(self, arr, n):
		arr.sort(reverse=True)
		return arr[0]


class Solution:

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


class Solution:

	def findMaximum(self, arr, n):
		l = len(arr)
		l = int(l / 2)
		max = arr[0]
		for i in range(n):
			if arr[i] > max:
				max = arr[i]
		return max


class Solution:

	def findMaximum(self, arr, n):
		max_val = arr[0]
		for x in arr:
			if x < max_val:
				break
			max_val = x
		return max_val


def findMaximumUtil(arr, low, high):
	if low == high:
		return arr[low]
	if high == low + 1 and arr[low] >= arr[high]:
		return arr[low]
	if high == low + 1 and arr[low] < arr[high]:
		return arr[high]
	mid = (low + high) // 2
	if arr[mid] > arr[mid + 1] and arr[mid] > arr[mid - 1]:
		return arr[mid]
	if arr[mid] > arr[mid + 1] and arr[mid] < arr[mid - 1]:
		return findMaximumUtil(arr, low, mid - 1)
	else:
		return findMaximumUtil(arr, mid + 1, high)

class Solution:

	def findMaximum(self, Arr, N):
		return findMaximumUtil(Arr, 0, N - 1)


class Solution:

	def findMaximum(self, arr, n):
		arr = sorted(arr)
		return arr[-1]


class Solution:

	def findMaximum(self, arr, n):
		max1 = float('-inf')
		for i in range(n):
			if max1 < arr[i]:
				max1 = arr[i]
		return max1


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

class Solution:

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


class Solution:

	def findMaximum(self, arr, n):
		c = 0
		for i in range(1, n - 1):
			if arr[i] < arr[i + 1]:
				continue
			else:
				c = arr[i]
				break
		b = arr
		b.sort()
		b = list(b)
		if b == arr:
			return arr[n - 1]
		else:
			return c


class Solution:

	def findMaximum(self, arr, n):

		def binSearch(i, j):
			if i == j:
				return arr[i]
			m = (i + j) // 2
			if arr[m] > arr[m + 1]:
				return binSearch(i, m)
			else:
				return binSearch(m + 1, j)
		return binSearch(0, n - 1)


class Solution:

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


class Solution:

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


class Solution:

	def findMaximum(self, arr, n):
		max = 0
		i = 0
		while i < n:
			if arr[i] >= max:
				max = arr[i]
			i = i + 1
		return max


class Solution:

	def findMaximum(self, arr, n):
		max = arr[0]
		for i in range(0, n):
			l = max
			if max < arr[i]:
				max = arr[i]
			else:
				max = l
		return max


class Solution:

	def findMaximum(self, arr, n):
		leftIndex = 0
		rightIndex = n - 1
		while leftIndex != rightIndex:
			if rightIndex - leftIndex == 1:
				if arr[rightIndex] > arr[leftIndex]:
					leftIndex = rightIndex
				else:
					rightIndex = leftIndex
				continue
			idx = (leftIndex + rightIndex) // 2
			if arr[idx - 1] < arr[idx]:
				leftIndex = idx
			if arr[idx] > arr[idx + 1]:
				rightIndex = idx
		return arr[leftIndex]


class Solution:

	def findMaximum(self, arr, n):
		for i in arr:
			return max(arr)


class Solution:

	def findMaximum(self, arr, n):
		for x in range(n):
			try:
				if x < n:
					if arr[x] > arr[x + 1]:
						return arr[x]
			except:
				return arr[x]


class Solution:

	def findMaximum(self, arr, n):
		b = 0
		c = arr[0]
		for i in arr[1:]:
			if i > c:
				c = i
		return c


class Solution:

	def findMaximum(self, arr, n):
		l = 0
		h = n - 1
		while l <= h:
			mid = (l + h) // 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] and arr[mid] < arr[mid - 1]:
				h = mid - 1
			else:
				l = mid + 1
		return arr[mid]


class Solution:

	def findMaximum(self, arr, n):
		a = arr
		low = 0
		high = n - 1
		return self.findMaximum1(a, low, high)

	def findMaximum1(self, a, low, high):
		if high == low:
			return a[high]
		if high == low + 1 and a[high] > a[low]:
			return a[high]
		if high == low + 1 and a[high] <= a[low]:
			return a[low]
		mid = (low + high) // 2
		if a[mid - 1] < a[mid] > a[mid + 1]:
			return a[mid]
		if a[mid] < a[mid - 1] and a[mid] > a[mid + 1]:
			return self.findMaximum1(a, low, mid - 1)
		else:
			return self.findMaximum1(a, mid + 1, high)


class Solution:

	def findMaximum(self, arr, n):
		max = 0
		for i in arr:
			if i > max:
				max = i


class Solution:

	def findMaximum(self, arr, n):
		maxi = 0
		for num in arr:
			if num > maxi:
				maxi = num
			else:
				return maxi
		return maxi


class Solution:

	def findMaximum(self, arr, n):
		if arr[-1] > arr[-2]:
			return arr[-1]
		(l, r, ans) = (0, n - 1, 0)
		while l <= r:
			mid = int((l + r) / 2)
			ans = max(ans, arr[mid])
			if arr[mid + 1] >= arr[mid]:
				l = mid + 1
			else:
				r = mid - 1
		return ans


class Solution:

	def findMaximum(self, arr, n):
		maxs = 0
		for i in arr:
			if i > maxs:
				maxs = i
		return maxs


class Solution:

	def findMaximum(self, arr, n):
		(l, r) = (0, n - 1)
		while l <= r:
			mid = l + (r - l) // 2
			if mid != 0 and mid != len(arr) - 1:
				if arr[mid - 1] < arr[mid] and arr[mid + 1] < arr[mid]:
					return arr[mid]
				elif arr[mid + 1] < arr[mid]:
					r = mid - 1
				else:
					l = mid + 1
			else:
				break
		return arr[-1]


class Solution:

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


class Solution:

	def findMaximum(self, arr, n):
		m = arr[0]
		for i in range(n - 1):
			m = arr[n - 1]
			if arr[i + 1] < arr[i]:
				m = arr[i]
				break
		return m


class Solution:

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


class Solution:

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