This problem asks us to arrange a deck of cards so that when we repeatedly take the top card and move the next card to the bottom, the revealed cards come out in sorted (increasing) order.
We know the reveal process (take top, move next to bottom, repeat) and the desired output (sorted order). The task is to work backwards from those two facts to the starting arrangement.
The reveal process is deterministic. For a given deck size, the position that gets revealed at each step is fixed, regardless of the values placed there. So the problem reduces to one question: in what order do the positions get visited? Once that order is known, we place the smallest value into the first position visited, the next smallest into the second, and so on.
1 <= deck.length <= 1000 -> With at most 1,000 cards, the simulation runs comfortably. The cost is dominated by sorting at O(n log n).1 <= deck[i] <= 10^6 -> Values fit in a 32-bit signed integer, so no overflow concerns and no negative values.Try every possible ordering of the deck, simulate the reveal process for each one, and check whether the revealed order is sorted. Return the first ordering that works.
Since values are unique, exactly one ordering produces a sorted reveal, so the search is guaranteed to find it. There are n! permutations of n cards, and each one costs O(n) to simulate. That limits this approach to tiny inputs, but it establishes what the answer should look like.
Input:
Sorted target: [2, 3, 5, 7, 11, 13, 17]
We try permutations until one produces the sorted order under the reveal process. The arrangement that works is [2, 13, 3, 11, 5, 17, 7]. Simulating its reveal confirms it: reveal 2 and move 13 to the bottom, reveal 3 and move 11 to the bottom, reveal 5 and move 17 to the bottom, reveal 7 and move 13 to the bottom, reveal 11 and move 17 to the bottom, reveal 13, reveal 17. The revealed sequence is 2, 3, 5, 7, 11, 13, 17, which is sorted, so this is the answer:
The brute force tries every arrangement without using any structure of the problem. The next approach computes the answer directly by simulating the reveal process on positions instead of values.
The reveal process is determined only by the number of cards, not by the values. For n cards, it always visits positions in the same order. So we can run the reveal process on the indices 0, 1, 2, ..., n-1 to discover that order, then drop sorted values into those positions.
Put indices 0 through n-1 into a queue and simulate the exact reveal process: dequeue the front (this position is revealed first), then dequeue-and-enqueue the next (move it to the bottom). The order in which indices come out is the order in which positions are revealed.
The mapping follows directly. Sort the deck values. The smallest value goes into the position revealed first, the second smallest into the position revealed second, and so on.
Approach 2 is optimal on time complexity. A second method reaches the same answer from the opposite direction: rather than simulating forward on indices, it reconstructs the deck backwards starting from the last revealed card.
Rather than figuring out where each sorted value goes, we reconstruct the deck backwards. Start with the largest card (the last one revealed) and undo the reveal steps one at a time.
In the forward process, each step does two things: reveal the top card, then move the next top card to the bottom. Undoing a step reverses both operations in the opposite order: move the bottom card back to the top, then place the just-revealed card on top.
So we start with an empty deque, iterate through the sorted values from largest to smallest, and for each value move the back of the deque to the front, then push the current value to the front. When the iteration finishes, the deque holds the answer.
Each forward step is invertible. Revealing the top card undoes to placing a card on top, and moving the next card to the bottom undoes to moving the bottom card to the top. Applying these inverses from the last revealed card back to the first reconstructs the deck before any reveal happened.
The order of the two inverse operations matters. In the forward step the move-to-bottom happens after the reveal, so when undoing we must reverse the move-to-bottom first (bring the bottom element back to the top) before placing the revealed card on top. Running the operations in that order reproduces exactly the deck state that the forward process would have started from.