bits holds only zeros and ones. In one swap you may exchange any two elements of the array, not necessarily neighbours.
Find the smallest number of swaps after which all the ones sit next to each other in a single unbroken block. Where that block ends up does not matter.
If there are no ones, or they are already together, the answer is 0.bits=[0, 1, 1, 0, 0, 1, 0] → 1
There are three ones. Swapping bits[3] and bits[5] gives [0, 1, 1, 1, 0, 0, 0], so one swap is enough.
bits=[1, 0, 0, 1, 0, 0, 1, 1] → 2
The best spot for the four ones is the last four cells: two ones are already there, and two swaps bring in the other two.
bits=[0, 0, 0] → 0
bits[i] is 0 or 1bits=[0, 1, 1, 0, 0, 1, 0]
1
Run checks the first 3 tests in your browser. Submit checks all 120 tests on the server, hidden ones included.