Distant Barcodes

In a warehouse, there is a row of barcodes, where the i^th barcode is barcodes[i].

Rearrange the barcodes so that no two adjacent barcodes are equal. You may return any answer, and it is guaranteed an answer exists.

Example 1
Inputbarcodes = [1,1,1,2,2,2]
Output[2,1,2,1,2,1]
The barcodes are rearranged so that every pair of adjacent values is different.
Example 2
Inputbarcodes = [1,1,1,1,2,2,3,3]
Output[1,3,1,3,1,2,1,2]
The barcodes are rearranged so that no two adjacent barcodes are equal.

Constraints

  • 1 <= barcodes.length <= 10000
  • 1 <= barcodes[i] <= 10000

Asked at 2 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate