Weird Sort
Time limit1sMemory limit128 MB
Reorder N integers so no element equals the previous element plus one, outputting the lexicographically smallest valid ordering or No solution.
- Level
Medium7 of 10
- Topics
- Greedy, Sorting, Math, Implementation
- Solved
- No attempts yet
Problem
You are given a sequence of integers . Reorder them so that no element is exactly one greater than the element immediately before it. Formally, the final sequence must satisfy for every with .
If several orderings satisfy this condition, output the lexicographically smallest one.
Input
The input consists of several data sets. Each data set spans two lines. The first line contains the sequence length (). The second line contains integers separated by single spaces, each satisfying . A line containing a single marks the end of the input and is not processed.
Output
For each data set, print the resulting sequence on its own line, with integers separated by single spaces. If no valid ordering exists, print No solution instead.