## J, 25 bytes [:(}.=>./)@;0<@(*#);.1@,] This is a monadic verb that takes and returns a 0-1 array. Use it like this: f =: [:(}.=>./)@;0<@(*#);.1@,] f 1 1 0 1 1 1 0 1 1 0 0 0 1 1 1 0 0 0 ## Explanation [:(}.=>./)@;0<@(*#);.1@,] Input is y. 0 ,] Prepend 0 to y, and ;.1@ cut the result along occurrences of 0, so that each piece begins with a 0. (*#) Multiply each piece element-wise by its length, <@ and put it in a box. Without the boxing, the pieces would go in a 0-padded array. ; Join the pieces back together. Now all runs of 1 has been replaced by runs of (1+length of run). ( )@ Apply verb in parentheses: }. remove the prepended 0, = form the 0-1 array of equality with >./ the maximum value.