educative.io

Next Greater Number Question

For input [4, 6, 3, 2, 8, 5, 11] , i get the answer as [6, 8, 8, 8, 11, 11, -1] . But the answer must be [5, 8, 5, 5,11,11, -1] . Please do clarify what is the expected output for this question

hey Shiva,

the answer would be [6, 8, 8, 8, 11, 11, -1] , since when you are in 4th position of the array you’r next biggest element is 8, this would be true for index 1,2,3.

Hi @shiva1,

This is Fatimah from Educative.

In this challenge by “next greater element” we mean that the first element we can find to the right of that element which is also greater than it. It does not mean the next greater element in the whole array or the right subarray even. If that were the case, using a stack would not make sense in the solution. @Kamala_sekar_Rose has also done a great job explaining it! Thank you.

Best regards,
Fatimah Abdullah | Developer Advocate
Educative

1 Like