#K36542. Minimum Swaps to Beautify Stamp Sequence
Minimum Swaps to Beautify Stamp Sequence
Minimum Swaps to Beautify Stamp Sequence
You are given a sequence of n stamps, each stamp having a color represented by an integer. Your task is to determine the minimum number of swaps required to rearrange the sequence so that no two adjacent stamps have the same color. A swap can exchange the positions of any two stamps from the sequence.
If it is impossible to achieve such a beautiful sequence, output \( -1 \).
Note: A sequence is considered beautiful if for every \( i \) (where \( 1 \leq i \leq n-1 \)), the condition \( a_i \neq a_{i+1} \) holds.
inputFormat
The input is given via standard input (stdin) in the following format:
n c1 c2 ... cn
Here, the first line contains a single integer \( n \) indicating the number of stamps. The second line contains \( n \) space-separated integers where each integer represents the color of a stamp.
outputFormat
Output a single integer to standard output (stdout) representing the minimum number of swaps required to achieve a beautiful sequence. If it is not possible, output \( -1 \).
## sample5
3 3 2 2 1
2