last
to -1, which will hold the index of the last seen 1
.maxDistance
to 0, which will hold the maximum distance found.n
from right to left (least significant bit to most significant bit).i
starting from 0.1
, check if last
is not -1 (which means this is not the first 1
found).1
, update maxDistance
with the maximum of maxDistance
and i - last
.last
to the current index i
.maxDistance
as the result.