AlgoMaster Logo

Reveal Cards In Increasing Order

deck=[17, 13, 11, 2, 3, 5, 7]
1public int[] deckRevealedIncreasing(int[] deck) {
2    Arrays.sort(deck);
3    int n = deck.length;
4
5    // Initialize queue with indices
6    Queue<Integer> queue = new LinkedList<>();
7    for (int i = 0; i < n; i++) {
8        queue.offer(i);
9    }
10    int[] result = new int[n];
11
12    // Process each card
13    for (int i = 0; i < n; i++) {
14        // Place card at front position
15        int idx = queue.poll();
16        result[idx] = deck[i];
17
18        // Move next position to back
19        if (!queue.isEmpty()) {
20            queue.offer(queue.poll());
21        }
22    }
23
24    return result;
25}
0 / 30
deck01711321132435567