Given an unsorted array of integers. Find an element such that all the elements to its left are smaller and to its right are greater. Print -1 if no such element exists. Note that there can be more than one such element. In that case print the first such number occurring in the array.
Example 1:
Input: N = 7, arr[] = {4, 3, 2, 5, 8, 6, 7} 
Output: 5
Explanation: To the left of element 5 
every element is smaller to it and to 
the right of element 5 every element 
is greater to it.  
Example 2:
Input: N = 7, arr[] = {5, 6, 2, 8, 10, 9, 8} 
Output: -1
Explanation: No such desired element is 
present in the array.
Your Task:
This is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function FindElement() that takes array arr and integer N as parameters and returns the desired output.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 10^{6}

Difficulty: EASY
Tags: Data structures
Skill types: Data structures
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)

Starter code:
# function for adding one to number 

class Solution:

    def FindElement(self,arr, N):

        # Your code goes here 

Solutions:
class Solution:

	def FindElement(self, arr, N):
		c = 0
		for i in range(N):
			c = 0
			for j in range(N):
				if j < i:
					if arr[j] >= arr[i]:
						c = 1
						break
				elif j == i:
					pass
				elif j > i:
					if arr[j] <= arr[i]:
						c = 1
						break
			if c == 0:
				return arr[i]
				break
		return -1


class Solution:

	def FindElement(self, arr, N):
		max_ele = arr[0]
		li = [max_ele]
		for i in range(1, N):
			if max_ele >= arr[i]:
				if len(li) > 0:
					if li[0] >= arr[i]:
						li.clear()
			else:
				max_ele = arr[i]
				li.append(arr[i])
		return li[0] if len(li) > 0 else -1


class Solution:

	def FindElement(self, arr, N):
		small_right = [0] * n
		big_left = [0] * n
		big_left[0] = arr[0]
		small_right[n - 1] = arr[n - 1]
		for i in range(1, n):
			if big_left[i - 1] < arr[i]:
				big_left[i] = arr[i]
			else:
				big_left[i] = big_left[i - 1]
		for i in range(n - 2, -1, -1):
			if small_right[i + 1] > arr[i]:
				small_right[i] = arr[i]
			else:
				small_right[i] = small_right[i + 1]
		for i in range(0, N, 1):
			if i == 0 and arr[i] < small_right[i + 1] or (i == N - 1 and arr[i] > big_left[i - 1]) or (arr[i] > big_left[i - 1] and arr[i] < small_right[i + 1]):
				return arr[i]
				break
		return -1


class Solution:

	def FindElement(self, arr, N):
		A = arr
		n = N
		SE = [0] * n
		GE = [0] * n
		GE[0] = A[0]
		SE[n - 1] = A[n - 1]
		for i in range(1, n):
			if GE[i - 1] < A[i]:
				GE[i] = A[i]
			else:
				GE[i] = GE[i - 1]
		for i in range(n - 2, -1, -1):
			if A[i] < SE[i + 1]:
				SE[i] = A[i]
			else:
				SE[i] = SE[i + 1]
		for j in range(n):
			try:
				if j == 0 and A[j] < SE[j + 1] or (j == n - 1 and A[j] > GE[j - 1]) or (A[j] < SE[j + 1] and A[j] > GE[j - 1]):
					return A[j]
			except:
				pass
		return -1


class Solution:

	def FindElement(self, arr, N):
		maxi = [arr[0]]
		mini = [arr[-1]]
		for i in range(1, N):
			maxi.append(max(maxi[-1], arr[i]))
			mini.append(min(mini[-1], arr[-i - 1]))
		mini.reverse()
		if arr[0] < mini[1]:
			return arr[0]
		for i in range(1, N - 1):
			if maxi[i - 1] < arr[i] < mini[i + 1]:
				return arr[i]
		if arr[-1] > maxi[-2]:
			return arr[-1]
		return -1


class Solution:

	def FindElement(self, arr, n):
		greather = [0] * n
		smaller = [0] * n
		greather[0] = arr[0]
		for i in range(1, n):
			greather[i] = max(greather[i - 1], arr[i])
		smaller[n - 1] = arr[n - 1]
		for i in range(n - 2, -1, -1):
			smaller[i] = min(smaller[i + 1], arr[i])
		if smaller[1] > arr[0]:
			return arr[0]
		for i in range(1, n - 1):
			if smaller[i + 1] > arr[i] and greather[i - 1] < arr[i]:
				return arr[i]
		if greather[n - 2] < arr[n - 1]:
			return arr[n - 1]
		return -1


import sys

class Solution:

	def FindElement(self, arr, N):
		min_index = [0] * N
		max_index = [0] * N
		minimum = arr[N - 1]
		min_index[N - 1] = N - 1
		maximum = arr[0]
		max_index[0] = 0
		ans = -1
		for i in range(1, N):
			if arr[i] > maximum:
				max_index[i] = i
				maximum = arr[i]
			else:
				max_index[i] = max_index[i - 1]
		for i in range(N - 2, -1, -1):
			if arr[i] < minimum:
				minimum = arr[i]
				min_index[i] = i
			else:
				min_index[i] = min_index[i + 1]
		for i in range(N):
			if min_index[i] == max_index[i]:
				ans = arr[i]
				break
		return ans


import sys

class Solution:

	def FindElement(self, arr, n):
		pre = [0] * n
		suf = [0] * n
		pre[0] = -sys.maxsize
		suf[n - 1] = sys.maxsize
		for i in range(1, n):
			pre[i] = max(pre[i - 1], arr[i - 1])
		for i in range(n - 2, -1, -1):
			suf[i] = min(suf[i + 1], arr[i + 1])
		for i in range(n):
			if pre[i] < arr[i] and arr[i] < suf[i]:
				return arr[i]
		return -1


class Solution:

	def FindElement(self, arr, n):
		(mx, mi, cur_max) = ([0 for i in range(n)], [0 for i in range(n)], -1)
		for i in range(0, n):
			mx[i] = cur_max
			cur_max = max(cur_max, arr[i])
		cur_min = cur_max + 1
		for i in range(n - 1, -1, -1):
			mi[i] = cur_min
			cur_min = min(cur_min, arr[i])
		if arr[0] < mi[0]:
			return arr[0]
		for i in range(1, n - 1):
			if mx[i] < arr[i] < mi[i]:
				return arr[i]
		if arr[-1] > mx[-1]:
			return arr[-1]
		return -1


class Solution:

	def FindElement(self, arr, n):
		for i in range(0, n):
			(left, right) = (1, 1)
			for j in range(0, i):
				if arr[j] >= arr[i]:
					left = 0
					break
			if left == 0:
				continue
			for j in range(i + 1, n):
				if arr[j] <= arr[i]:
					right = 0
					break
			if right == 1:
				return arr[i]
		return -1


class Solution:

	def FindElement(self, arr, N):
		left = []
		right = []
		maximum = arr[0]
		for i in arr:
			maximum = max(i, maximum)
			left.append(maximum)
		minimum = arr[-1]
		for i in arr[::-1]:
			minimum = min(i, minimum)
			right.append(minimum)
		right = right[::-1]
		if arr[0] < right[1]:
			return arr[0]
		ans = -1
		for i in range(1, N - 1):
			if arr[i] > left[i - 1] and arr[i] < right[i + 1]:
				ans = arr[i]
				break
		if ans != -1:
			return ans
		if left[-2] < arr[-1]:
			return arr[-1]
		return -1


class Solution:

	def FindElement(self, arr, N):
		ge = [0] * N
		se = [0] * N
		ge[0] = arr[0]
		se[N - 1] = arr[N - 1]
		for i in range(1, N):
			if arr[i] > ge[i - 1]:
				ge[i] = arr[i]
			else:
				ge[i] = ge[i - 1]
		for i in range(N - 2, -1, -1):
			if arr[i] < se[i + 1]:
				se[i] = arr[i]
			else:
				se[i] = se[i + 1]
		for i in range(N):
			if i == 0 and se[i + 1] > arr[i]:
				return arr[i]
			elif i == N - 1 and arr[N - 1] > ge[i - 1]:
				return arr[i]
			elif arr[i] > ge[i - 1] and arr[i] < se[i + 1]:
				return arr[i]
		return -1


class Solution:

	def FindElement(self, arr, N):
		mx = arr[N - 1] + 1
		amx = [0] * N
		mn = arr[0] - 1
		amn = [0] * N
		if arr[0] >= arr[N - 1]:
			return -1
		for i in range(N):
			amx[N - 1 - i] = mx
			if mx > arr[N - 1 - i]:
				mx = arr[N - 1 - i]
			amn[i] = mn
			if mn < arr[i]:
				mn = arr[i]
		for i in range(N):
			if amx[i] > arr[i] and arr[i] > amn[i]:
				return arr[i]
		return -1


class Solution:

	def FindElement(self, arr, N):
		mina = []
		maxa = []
		m = -99999999
		c = 0
		for i in range(N):
			if arr[i] > m:
				m = arr[i]
				maxa.append(m)
			elif arr[i] == m:
				maxa.append(c)
				c += 2
			else:
				maxa.append(m)
		m = 999999999
		for i in range(N - 1, -1, -1):
			if arr[i] < m:
				m = arr[i]
				mina.append(m)
			elif arr[i] == m:
				mina.append(c)
				c += 1
			else:
				mina.append(m)
		mina = mina[::-1]
		for i in range(N):
			if maxa[i] == mina[i]:
				return maxa[i]
		return -1
if __name__ == '__main__':
	t = int(input())
	for _ in range(0, t):
		n = int(input())
		a = list(map(int, input().split()))
		ob = Solution()
		print(ob.FindElement(a, n))


class Solution:

	def FindElement(self, arr, N):
		prefix = [-1000000000]
		suffix = [10000000 for i in range(N)]
		for i in range(1, N):
			prefix.append(max(prefix[i - 1], arr[i - 1]))
		for i in range(N - 2, -1, -1):
			suffix[i] = min(suffix[i + 1], arr[i + 1])
		for i in range(0, N):
			if prefix[i] < arr[i] and arr[i] < suffix[i]:
				return arr[i]
		return -1


class Solution:

	def FindElement(self, arr, N):
		GE = [0] * N
		SE = [0] * N
		GE[0] = arr[0]
		SE[N - 1] = arr[N - 1]
		for i in range(1, N):
			if arr[i] < GE[i - 1]:
				GE[i] = GE[i - 1]
			else:
				GE[i] = arr[i]
		for i in range(N - 2, -1, -1):
			if arr[i] > SE[i + 1]:
				SE[i] = SE[i + 1]
			else:
				SE[i] = arr[i]
		for j in range(N):
			k = arr[j]
			if j == 0:
				if arr[j] < SE[j + 1]:
					return k
			elif j == N - 1:
				if arr[j] > GE[j - 1]:
					return k
			elif arr[j] < SE[j + 1] and arr[j] > GE[j - 1]:
				return k
		return -1


class Solution:

	def FindElement(self, arr, N):
		maxLeft = []
		minRight = []
		for i in range(N):
			if not maxLeft:
				maxLeft.append(arr[i])
				minRight.append(arr[N - i - 1])
			else:
				maxLeft.append(max(maxLeft[-1], arr[i]))
				minRight.append(min(minRight[-1], arr[N - i - 1]))
		for i in range(N):
			if i == 0:
				if arr[i] < minRight[N - i - 2]:
					return arr[i]
			elif i == N - 1:
				if arr[i] > maxLeft[i - 1]:
					return arr[i]
			elif maxLeft[i - 1] < arr[i] and arr[i] < minRight[N - i - 2]:
				return arr[i]
		return -1


class Solution:

	def FindElement(self, arr, N):
		n = N
		GEL = [0] * n
		SER = [0] * n
		GEL[0] = arr[0]
		SER[n - 1] = arr[n - 1]
		for i in range(1, n):
			if arr[i] > GEL[i - 1]:
				GEL[i] = arr[i]
			else:
				GEL[i] = GEL[i - 1]
		for i in range(n - 2, -1, -1):
			if SER[i + 1] > arr[i]:
				SER[i] = arr[i]
			else:
				SER[i] = SER[i + 1]
		for i in range(0, n - 1):
			if i == 0 and arr[i] < SER[i + 1] or (arr[i] < SER[i + 1] and arr[i] > GEL[i - 1]):
				return arr[i]
		if arr[n - 1] > GEL[n - 2]:
			return arr[n - 1]
		else:
			return -1