Top MNC interview question which will be very useful for beginners to crack the interview and for there future careers

test

Breaking

Post Top Ad

Your Ad Spot

Saturday, December 26, 2020

Amazon Interview Question(Next Greater Element NGE)

 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.

For Example:

Array [10 ,43 ,33 ,99]

    Result:

            10 -- 43

            43 -- 99

            33 -- 99

            99 -- -1

Program:

def printNGE(array):

    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)

                                                                         ---------End---------

Post Top Ad

Your Ad Spot