Given a binary array, find the index of 0 to be replaced with 1 to get a maximum length sequence of continuous ones.

For example, consider array { 0, 0, 1, 0, 1, 1, 1, 0, 1, 1 }. The index to be replaced is 7 to get a continuous sequence of length 6 containing all 1’s.

Practice this problem

 
We have already discussed two approaches to solve this problem (here and here). In this post, another approach is discussed, which uses a sliding window technique.

 
The idea is to maintain a window containing at most one zero at any point and add elements to the window from the right until it becomes unstable. The window becomes unstable if the total number of zeros in it becomes 2. If the window becomes unstable, remove elements from its left till it becomes stable again (by removing the leftmost zero). If the window is stable and the current window length is more than the maximum window found so far, update the index of 0 to be replaced.

The algorithm can be implemented as follows in C, Java, and Python:

C


Download  Run Code

Output:

Index to be replaced is 7

Java


Download  Run Code

Output:

Index to be replaced is 7

Python


Download  Run Code

Output:

Index to be replaced is 7

The time complexity of the above solution is O(n) and doesn’t require any extra space, where n is the size of the given sequence.