FunctionRacerProblemsRace

Slide K Ones Together

HardArraysPython, JavaScript, TypeScript, Java, C++

bits holds zeros and ones, and at least k of its items are ones. One move makes two items that are next to each other trade places.

Return the fewest moves after which some k ones stand in a row with nothing between them.

Example 1

bits [1,0,1,0,1]
k 2
returns 1

Slide the last 1 one place to the left: 1, 0, 1, 1, 0. The middle two items are now ones in a row.

Example 2

bits [1,0,0,1,0,0,1]
k 3
returns 4

Move the first 1 two places right and the last 1 two places left, so all three meet around the middle one: 4 moves.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Arrays problems