positions
to store the indices of 1
s in the input array nums
.nums
and for each 1
found, append its index to positions
.prefixSum
with an additional leading 0
to simplify calculations.prefixSum
by adding the current element in positions
to the cumulative sum.minMoves
to infinity to track the minimum number of moves.k
to iterate over positions
.1
s to the median index.minMoves
with the minimum value between the current minMoves
and the calculated distance for the current window.minMoves
as the result.