Reveal Cards In Increasing Order - Problem

You are given an integer array deck. There is a deck of cards where every card has a unique integer. The integer on the ith card is deck[i].

You can order the deck in any order you want. Initially, all the cards start face down (unrevealed) in one deck.

You will do the following steps repeatedly until all cards are revealed:

  1. Take the top card of the deck, reveal it, and take it out of the deck.
  2. If there are still cards in the deck, then put the next top card of the deck at the bottom of the deck.
  3. If there are still unrevealed cards, go back to step 1. Otherwise, stop.

Return an ordering of the deck that would reveal the cards in increasing order.

Note that the first entry in the answer is considered to be the top of the deck.

Input & Output

Example 1 — Basic Case
$ Input: deck = [17,13,11,2,3,5,7]
Output: [2,13,3,11,5,17,7]
💡 Note: When we follow the reveal process: reveal 2, move 13 to bottom → reveal 3, move 11 to bottom → reveal 5, move 17 to bottom → reveal 7, move 13 to bottom → reveal 11, move 17 to bottom → reveal 13 → reveal 17. The revealed order is [2,3,5,7,11,13,17] which is increasing.
Example 2 — Small Deck
$ Input: deck = [1,2,3,4]
Output: [1,3,2,4]
💡 Note: Reveal process: reveal 1, move 3 to bottom → reveal 2, move 4 to bottom → reveal 3, move 4 to bottom → reveal 4. Result: [1,2,3,4] in order.
Example 3 — Two Cards
$ Input: deck = [1,2]
Output: [1,2]
💡 Note: With two cards: reveal 1, move 2 to bottom → reveal 2. Result: [1,2] which is already in order.

Constraints

  • 1 ≤ deck.length ≤ 1000
  • 1 ≤ deck[i] ≤ 106
  • All the values of deck are unique.

Visualization

Tap to expand
Card Reveal ProcessInitial Deck (face-down)2133Top → BottomReveal ProcessRevealed Cards (face-up)2357In Increasing Order!Step-by-step: Reveal top → Move next to bottom → RepeatGoal: Find arrangement [2,13,3,11,5,17,7]that reveals cards as [2,3,5,7,11,13,17]
Understanding the Visualization
1
Initial Deck
Cards arranged face-down: [2,13,3,11,5,17,7]
2
Reveal Process
Take top, reveal it, move next top to bottom
3
Final Result
Revealed cards: [2,3,5,7,11,13,17] in order
Key Takeaway
🎯 Key Insight: Work backwards from sorted order using reverse queue operations
Asked in
Google 15 Amazon 12 Microsoft 8
89.6K Views
Medium Frequency
~25 min Avg. Time
2.8K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen