Python/data_structures/stacks/next_greater_element.py

17 lines
390 B
Python
Raw Normal View History

2018-10-19 12:48:28 +00:00
# Function to print element and NGE pair for all elements of list
def printNGE(arr):
2018-10-19 12:48:28 +00:00
for i in range(0, len(arr), 1):
2018-10-19 12:48:28 +00:00
next = -1
for j in range(i+1, len(arr), 1):
if arr[i] < arr[j]:
next = arr[j]
break
2018-10-19 12:48:28 +00:00
print(str(arr[i]) + " -- " + str(next))
2018-10-19 12:48:28 +00:00
# Driver program to test above function
arr = [11,13,21,3]
printNGE(arr)