← Back to challenges

Indices of Zeroes for the Longest Run of Contiguous Ones

PythonHardarraysloops

Instructions

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).

python3
Loading editor…
to run
Walks through the solution with reasoning and edge cases.