AlgoMaster Logo

Reveal Cards In Increasing Order

mediumFrequency7 min readUpdated June 23, 2026

Understanding the Problem

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.

Key Constraints:

  • 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.
  • All values are unique -> The sorted order is strict, so there is exactly one correct arrangement and no tie-breaking is needed.

Approach 1: Brute Force (Try All Permutations)

Intuition

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.

Algorithm

  1. Sort the deck to get the target reveal order.
  2. Generate all permutations of the deck.
  3. For each permutation, simulate the reveal process (take top, move next to bottom, repeat).
  4. If the simulated reveal order matches the sorted order, return this permutation.

Example Walkthrough

Input:

0
17
1
13
2
11
3
2
4
3
5
5
6
7
deck

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:

0
2
1
13
2
3
3
11
4
5
5
17
6
7
result

Code

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.

Approach 2: Queue Simulation (Optimal)

Intuition

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.

Algorithm

  1. Sort the deck values in increasing order.
  2. Create a queue containing indices 0, 1, 2, ..., n-1.
  3. Simulate the reveal process on this queue of indices:
    • Dequeue the front index. This is the position revealed at this step.
    • If the queue is not empty, dequeue the next front index and enqueue it to the back (simulating "move to bottom").
  4. As you dequeue each index, assign the next sorted value to that position in the result array.
  5. Return the result array.

Example Walkthrough

1Initialize: sorted=[2,3,5,7,11,13,17], queue=[0,1,2,3,4,5,6]
0
_
1
_
2
_
3
_
4
_
5
_
6
_
1/8

Code

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.

Approach 3: Reverse Simulation with Deque

Intuition

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.

Algorithm

  1. Sort the deck in increasing order.
  2. Initialize an empty deque.
  3. Iterate from the largest value down to the smallest:
    • If the deque is not empty, move the element at the back of the deque to the front.
    • Push the current value to the front of the deque.
  4. Convert the deque to an array and return it.

Example Walkthrough

1Sorted: [2,3,5,7,11,13,17]. Start from largest, build backwards.
Front
Rear
1/8

Code