From 46b82fa249a0cb4811b3bfcbe99b68c32e33181f Mon Sep 17 00:00:00 2001 From: rajnishyadav321 Date: Thu, 5 Oct 2017 01:40:12 +0530 Subject: [PATCH] Added Next Greater Element Element NGE 4 --> 5 5 --> 25 2 --> 25 25 --> -1 --- data_structures/Stacks/next.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 data_structures/Stacks/next.py diff --git a/data_structures/Stacks/next.py b/data_structures/Stacks/next.py new file mode 100644 index 000000000..9765900c0 --- /dev/null +++ b/data_structures/Stacks/next.py @@ -0,0 +1,16 @@ +# Function to print element and NGE pair for all elements of list +def printNGE(arr): + + for i in range(0, len(arr), 1): + + next = -1 + for j in range(i+1, len(arr), 1): + if arr[i] < arr[j]: + next = arr[j] + break + + print(str(arr[i]) + " -- " + str(next)) + +# Driver program to test above function +arr = [11,13,21,3] +printNGE(arr)