Question:
Find the Next Greater Element in a given Array
Explanation:
We need print the Next Greater Element (NGE) for every element. The Next greater Element for an element x is the first greater element on the right side of x in array. Elements for which no greater element exist, consider next greater element as -1.
Result:
10 -- 43
43 -- 99
33 -- 99
99 -- -1
Program:
for i in range(0, len(array), 1):
next = -1
for j in range( i +1, len(array), 1):
if array[i] < array[j]:
next = array[j]
break
print(str(array[i]) + " -- " + str(next))
arr = [10 ,43 ,33 ,99]
printNGE(arr)