Next greater element for every value
Coding problemMediumAsked in: Adobe, Amazon, Flipkart, MakeMyTrip, Microsoft
Given a list of non-negative integers, return a list of the same length. At each position, put the first value to the right of that position that is strictly greater than the value there. If there is no such value, put -1.
Examples
Example 1
Input: nums = [4,5,2,25]
Output: [5,25,25,-1]
Example 2
Input: nums = [13,7,6,12]
Output: [-1,12,12,-1]
Example 3
Input: nums = [3,3,3]
Output: [-1,-1,-1]
An equal value is not greater.
Constraints
- The list holds between 0 and 300,000 values
- Each value is between 0 and 1,000,000,000
Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.
Run your code to see the results here. Ctrl or Cmd + Enter runs; add Shift to submit.