You hold n cards with distinct integer values. You want to arrange them into a single face-down deck (a top-to-bottom order) so that the following reveal process outputs the values in strictly increasing order.
Reveal process (the deck behaves as a queue; the top is the front):
Exactly one top-to-bottom arrangement makes the revealed sequence increasing. Output that arrangement.
Line 1: an integer n.
Line 2: n space-separated distinct integers, the card values in any order.
One line with n space-separated integers: the deck from top to bottom that reveals in increasing order.
Example 1
Input
5 1 2 3 4 5
Expected
1 5 2 4 3
Explanation
The deck 1 5 2 4 3 reveals as: show 1, move 5 to bottom; show 2, move 4 to bottom; show 3, move 5; show 4, move 5; show 5 - giving 1 2 3 4 5.
Example 2
Input
1 42
Expected
42
Explanation
A single card is already in increasing order, so the deck is just 42.
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 →