How to get the next greater element in order?

I have approached this problem using stack I am able to get all the next greater element corresponding to every element but not in order. I was thinking of taking a map and store each element with it’s NGE but this would work only when array contains distinct element. As it’s not mentioned in the problem statement that array will contain unique element so I am not going with this approach. Any help would be appreciated. Thanks in advance :’)

Hey you do not need to have a map to achieve the order, see how you can play with indices and stack so that order is maintained.


@tusharanand Is this resolved?

@tusharanand we discussed this question yesterday in DMs. The issue was occuring due to using stoi to convert to long long instead of stoll. Also repeated string to long long to string conversion was making it more ineffecient. Once that was fixed it passed all cases.

Closing this topic as your issue is resolved by the mentor. If it is still not resolved, Kindly un-mark the accepted solution or create a new topic and post this question as a reference link in the description of the new topic.