#K58442. Rearrange Plants
Rearrange Plants
Rearrange Plants
Given an integer n and an array of n integers representing the heights of plants, your task is to rearrange the plants such that no two adjacent plants have the same height. If such a rearrangement is possible, output the rearranged sequence; otherwise, output Not possible
.
The algorithm should determine whether a valid ordering exists. If yes, it will output one valid ordering (the ordering is unique based on the greedy algorithm used). Otherwise, it outputs a failure message.
Note: All formulas, if any, are to be written in LaTeX format. For example: $$a \neq b$$.
inputFormat
The input is given via stdin in the following format:
n h1 h2 h3 ... hn
Where:
n
is an integer representing the number of plants.h1, h2, ..., hn
are the plant heights separated by spaces.
outputFormat
Output the rearranged plant heights in one line separated by single spaces if a valid rearrangement exists. Otherwise, output Not possible
(without quotes). The output should be printed to stdout.
5
3 1 3 2 2
2 3 1 2 3
</p>