Question
The Librarian's Empty Shelf Slots
A librarian is organizing a single shelf that holds
Rearrange the shelf in-place so that every empty slot moves to the end, while every book keeps its original relative order among the other books.
You must do this using only constant extra space.
Input
The first line contains a single integer KaTeX can only parse string typed expression — the number of slots.
The second line contains KaTeX can only parse string typed expression integers (this line may be empty if KaTeX can only parse string typed expression).
Output
Print the rearranged shelf as KaTeX can only parse string typed expression space-separated integers.
Example
Example 1:
Input
The books with catalog IDs 1, 3, and 12 keep their original relative order, and both empty slots move to the end.
Example 2:
Input
The entire shelf is already empty, so it stays unchanged.
Input
5
0 1 0 3 12Output1 3 12 0 0ExplanationThe books with catalog IDs 1, 3, and 12 keep their original relative order, and both empty slots move to the end.
Example 2:
Input
4
0 0 0 0Output0 0 0 0ExplanationThe entire shelf is already empty, so it stays unchanged.