Rearrange a deck so that repeatedly revealing the top card produces the cards in strictly increasing order.
Problem
You are given an array of unique integers representing a deck of cards. Your task is to reorder the deck so that if you perform the following process, the revealed cards appear in increasing order:
- Reveal the top card and remove it from the deck.
- If any cards remain, move the next top card to the bottom of the deck.
- Repeat until all cards are revealed.
Return one valid initial ordering of the deck that makes the revealed sequence sorted from smallest to largest.
Notes
- The input values are distinct.
- Any valid arrangement that satisfies the reveal process is acceptable.
Input Format
- A single array of distinct integers
deck.
The array represents the card values, not their initial positions.
Output Format
- Return an array representing the initial deck order that causes the reveal process to output the cards in increasing order.
Constraints
1 <= deck.length- All values in
deckare distinct. - The answer should be a permutation of the input values.
Example 1
Input
deck = [17,13,11,2,3,5,7]
Output
[2,13,3,11,5,17,7]
Explanation
If you reveal from the top and move the next card to the bottom repeatedly, the revealed sequence is [2,3,5,7,11,13,17].
Example 2
Input
deck = [1,1000]
Output
[1,1000]
Explanation
The deck already reveals in increasing order.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.