An auction house lines up n lots for a single sale, displayed in the exact order buyers will see them, each carrying a listed price. As a closing-day promotion, every lot's price is discounted by the price of the nearest lot appearing later in the line whose price is no higher than its own. A lot that has no such later lot -- because every lot after it costs strictly more, or it is the last lot in the line -- keeps its full listed price. Determine the final price of every lot once this single discount rule has been applied.
Print n space-separated integers: the final price of each lot, in the same order, after its discount (if any) has been applied.
Example 1
Input
7 8 4 6 2 3 8 4
Expected
4 2 4 2 3 4 4
Explanation
Lot0 costs 8; the nearest later lot priced <=8 is lot1 (4), giving 8-4=4. Lot1 costs4; lot2 (6) is too high but lot3 (2) qualifies, giving 4-2=2. Lot2 costs6; lot3 (2) qualifies, giving 6-2=4. Lot3 costs2; lots4,5,6 (3,8,4) all cost more, so it keeps 2. Lot4 costs3; lots5,6 (8,4) both cost more, so it keeps 3. Lot5 costs8; lot6 (4) qualifies, giving 8-4=4. Lot6 is last, so it keeps its price 4. Final prices: 4 2 4 2 3 4 4.
Example 2
Input
5 1 2 3 4 5
Expected
1 2 3 4 5
Explanation
The prices strictly increase from left to right, so no lot ever has a later lot priced at or below its own price -- no discount ever applies, and every lot keeps its listed price unchanged.
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 →