Ctrl K

Hopping Tokens Together

Easy
Tokens sit on an endless strip of cells; spots[i] is the cell where token i stands. Several tokens may share a cell. In one move any token can be shifted: - 2 cells left or right for free; - 1 cell left or right for 1 coin. All tokens must end up on the same cell, which you get to choose. Return the minimum number of coins this takes.
Example 1
spots=[2, 4, 5, 7, 9] → 2

The tokens on 5, 7 and 9 reach cell 5 for free in steps of two, the tokens on 2 and 4 reach cell 4 for free and then each pays for one step to 5. The total is 2.

Example 2
spots=[3, 3, 10] → 1

Gathering on cell 3 is cheapest: the token on 10 walks to cell 4 for free and pays for one step. The answer is 1.

Example 3
spots=[6] → 0
Constraints
  • 1 ≤ len(spots) ≤ 10⁵
  • 1 ≤ spots[i] ≤ 10⁹
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTriangle From Sticks EasyFewest Guests by Lanyard Color MediumUp or Down for Every Post MediumRound Prices to a Total MediumCloning Builders Hard
loading Python…
Input
spots=[2, 4, 5, 7, 9]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 100 tests on the server, hidden ones included.