An online auction records n bids in the order they arrived, each a positive integer amount. For every bid, scan forward (toward later bids) and find the nearest later bid whose amount is strictly greater than the current bid's amount. Report that greater amount, or -1 if no later bid is strictly higher.
Line 1: an integer n.
Line 2: n space-separated integers, the bid amounts in arrival order.
n space-separated integers on one line: for each bid (in arrival order), the amount of the nearest later strictly-higher bid, or -1.
Example 1
Input
5 4 2 6 3 5
Expected
6 6 -1 5 -1
Explanation
Bid 4: the nearest later higher bid is 6. Bid 2: nearest later higher is 6. Bid 6: no later bid exceeds 6, so -1. Bid 3: nearest later higher is 5. Bid 5: no later bid exceeds 5, so -1.
Example 2
Input
3 7 7 7
Expected
-1 -1 -1
Explanation
Every later bid equals 7, and equal is not strictly higher, so each answer is -1.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →