Indices of Zeroes for the Longest Run of Contiguous Ones

Published by Helen Yu in

You are given a list of binary integers and k, the number of flips allowed.

Write a function that returns the indices of zeroes of which, when flipped, return the longest contiguous sequence of ones.

Examples

zero_indices([1, 0, 1, 1, 0, 0, 0, 1], 1) ➞ [1]

zero_indices([1, 0, 0, 0, 0, 1], 1) ➞ [1]

zero_indices([1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0], 3) ➞ [6, 7, 9]

zero_indices([1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0], 3) ➞ [7, 8, 9]

Notes

If multiple combinations of indices tie for longest one sequence, return the indices which occur first (see examples #2, #3).

Watch a quick demo on how Edabit works.