Count how many element it has. We don’t care about the number in array. We only care about the index. If there are 5 elements, we will need to a figure out the result for [0, 1, 2, 3, 4](which is actually [0, 4, 1, 3, 2]).
Then, we sort the input array, and reorder the deck[] according to index sequence.
Reverse process is like below:
public static int[] deckRevealedIncreasing(int[] deck) { int[] ans = new int[deck.length]; Deque<Integer> deque = new LinkedList<>(); deque.addLast(deck.length - 1); for (int i = deck.length - 2; i >= 0; i--) { deque.addFirst(deque.removeLast()); deque.addFirst(i); } Arrays.sort(deck); for (int i = 0; i < ans.length; i++) { ans[i] = deck[deque.removeFirst()]; } return ans; }