the art of
Algorithm
Notes on Analysis and Design



Next Greater Element

Given an array, 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
def NGE(x):
	flag=False
	for i in arr[arr.count(x):]:
		if i>x:
			print i
			flag=True
	if !flag:
		print "-1"

arr=[4,5,2,25]
x=25
NGE(x)