Description

Given an array of barcodes, rearrange them so that no two adjacent barcodes are equal. You can assume valid arrangement exists.

Examples

Input:barcodes = [1,1,1,2,2,2]
Output:[2,1,2,1,2,1]
Explanation:

Alternating arrangement.

Input:barcodes = [1]
Output:[1]
Explanation:

Edge case with a single-element array.

Input:barcodes = [1,1,1,1,2,2,3]
Output:[1,2,1,3,1,2,1]
Explanation:

When one barcode appears much more frequently than others, placing the most frequent barcode (1) in every other position first, then fill remaining positions with less frequent barcodes (2,3) to ensure no two adjacent barcodes are the same.

Constraints

  • 1 ≤ barcodes.length ≤ 10000

Ready to solve this problem?

Practice solo or challenge other developers in a real-time coding battle!