Next Greater Element
Advanced
In this challenge, you'll implement a function called next_greater_element. Imagine you have a list of numbers. For each number in the list, you want to find the first number to the right in the list that is greater than that number. If there is no such number, return -1 for that position.
Here's how it works:
- You look at each element in the list.
- Check the numbers to its right.
- Find the first number that is greater.
- If no such number exists, return -1 for that position.
Let's look at an example:
next_greater_element([4, 5, 2, 10]) # Output: [5, 10, 10, -1]
In this example:
- For 4, the next greater number is 5.
- For 5, it's 10.
- For 2, it's 10 again.
- For 10, there is no greater number to its right, so we return -1.
Another example:
next_greater_element([3, 8, 4, 2, 1, 7]) # Output: [8, -1, 7, 7, 7, -1]
Have fun finding the next greater element!
Inputs
Arg 1:
[4,5,2,10]Expected Output
[5, 10, 10, -1]Actual Output