# Find zeroes to be flipped so that number of consecutive 1’s is maximized

Given a binary array and an integer m, find the position of zeroes flipping which creates maximum number of consecutive 1’s in array.

**Examples :**

Input: arr[] = {1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1} m = 2 Output: 5 7 We are allowed to flip maximum 2 zeroes. If we flip arr[5] and arr[7], we get 8 consecutive 1's which is maximum possible under given constraints Input: arr[] = {1, 0, 0, 1, 1, 0, 1, 0, 1, 1, 1} m = 1 Output: 7 We are allowed to flip maximum 1 zero. If we flip arr[7], we get 5 consecutive 1's which is maximum possible under given constraints. Input: arr[] = {0, 0, 0, 1} m = 4 Output: 0 1 2 Since m is more than number of zeroes, we can flip all zeroes.

Source: http://www.careercup.com/question?id=5106425965576192

A **Simple Solution** is to consider every subarray by running two loops. For every subarray, count number of zeroes in it. Return the maximum size subarray with m or less zeroes. Time Complexity of this solution is O(n^{2}).

A **Better Solution** is to use auxiliary space to solve the problem in O(n) time.

For all positions of 0’s calculate left[] and right[] which defines the number of consecutive 1’s to the left of i and right of i respectively.

For example, for arr[] = {1, 1, 0, 1, 1, 0, 0, 1, 1, 1} and m = 1, left[2] = 2 and right[2] = 2, left[5] = 2 and right[5] = 0, left[6] = 0 and right[6] = 3.

left[] and right[] can be filled in O(n) time by traversing array once and keeping track of last seen 1 and last seen 0. While filling left[] and right[], we also store indexes of all zeroes in a third array say zeroes[]. For above example, this third array stores {2, 5, 6}

Now traverse zeroes[] and for all consecutive m entries in this array, compute the sum of 1s that can be produced. This step can be done in O(n) using left[] and right[].

An **Efficient Solution** can solve the problem in O(n) time and O(1) space. The idea is to use Sliding Window for the given array. The solution is taken from here.

Let us use a window covering from index wL to index wR. Let the number of zeros inside the window be zeroCount. We maintain the window with at most m zeros inside.

The main steps are:

– While zeroCount is no more than m: expand the window to the right (wR++) and update the count zeroCount.

– While zeroCount exceeds m, shrink the window from left (wL++), update zeroCount;

– Update the widest window along the way. The positions of output zeros are inside the best window.

Below is the implementation of the idea.

## C++

`// C++ program to find positions of zeroes flipping which ` `// produces maximum number of xonsecutive 1's ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// m is maximum of number zeroes allowed to flip ` `// n is size of array ` `void` `findZeroes(` `int` `arr[], ` `int` `n, ` `int` `m) ` `{ ` ` ` `// Left and right indexes of current window ` ` ` `int` `wL = 0, wR = 0; ` ` ` ` ` `// Left index and size of the widest window ` ` ` `int` `bestL = 0, bestWindow = 0; ` ` ` ` ` `// Count of zeroes in current window ` ` ` `int` `zeroCount = 0; ` ` ` ` ` `// While right boundary of current window doesn't cross ` ` ` `// right end ` ` ` `while` `(wR < n) ` ` ` `{ ` ` ` `// If zero count of current window is less than m, ` ` ` `// widen the window toward right ` ` ` `if` `(zeroCount <= m) ` ` ` `{ ` ` ` `if` `(arr[wR] == 0) ` ` ` `zeroCount++; ` ` ` `wR++; ` ` ` `} ` ` ` ` ` `// If zero count of current window is more than m, ` ` ` `// reduce the window from left ` ` ` `if` `(zeroCount > m) ` ` ` `{ ` ` ` `if` `(arr[wL] == 0) ` ` ` `zeroCount--; ` ` ` `wL++; ` ` ` `} ` ` ` ` ` `// Updqate widest window if this window size is more ` ` ` `if` `((wR-wL > bestWindow) && (zeroCount<=m)) ` ` ` `{ ` ` ` `bestWindow = wR-wL; ` ` ` `bestL = wL; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print positions of zeroes in the widest window ` ` ` `for` `(` `int` `i=0; i<bestWindow; i++) ` ` ` `{ ` ` ` `if` `(arr[bestL+i] == 0) ` ` ` `cout << bestL+i << ` `" "` `; ` ` ` `} ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `int` `arr[] = {1, 0, 0, 1, 1, 0, 1, 0, 1, 1}; ` ` ` `int` `m = 2; ` ` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]); ` ` ` `cout << ` `"Indexes of zeroes to be flipped are "` `; ` ` ` `findZeroes(arr, n, m); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`//Java to find positions of zeroes flipping which ` `// produces maximum number of consecutive 1's ` `class` `Test ` `{ ` ` ` `static` `int` `arr[] = ` `new` `int` `[]{` `1` `, ` `0` `, ` `0` `, ` `1` `, ` `1` `, ` `0` `, ` `1` `, ` `0` `, ` `1` `, ` `1` `}; ` ` ` ` ` `// m is maximum of number zeroes allowed to flip ` ` ` `static` `void` `findZeroes(` `int` `m) ` ` ` `{ ` ` ` `// Left and right indexes of current window ` ` ` `int` `wL = ` `0` `, wR = ` `0` `; ` ` ` ` ` `// Left index and size of the widest window ` ` ` `int` `bestL = ` `0` `, bestWindow = ` `0` `; ` ` ` ` ` `// Count of zeroes in current window ` ` ` `int` `zeroCount = ` `0` `; ` ` ` ` ` `// While right boundary of current window doesn't cross ` ` ` `// right end ` ` ` `while` `(wR < arr.length) ` ` ` `{ ` ` ` `// If zero count of current window is less than m, ` ` ` `// widen the window toward right ` ` ` `if` `(zeroCount <= m) ` ` ` `{ ` ` ` `if` `(arr[wR] == ` `0` `) ` ` ` `zeroCount++; ` ` ` `wR++; ` ` ` `} ` ` ` ` ` `// If zero count of current window is more than m, ` ` ` `// reduce the window from left ` ` ` `if` `(zeroCount > m) ` ` ` `{ ` ` ` `if` `(arr[wL] == ` `0` `) ` ` ` `zeroCount--; ` ` ` `wL++; ` ` ` `} ` ` ` ` ` `// Update widest window if this window size is more ` ` ` `if` `((wR-wL > bestWindow) && (zeroCount<=m)) ` ` ` `{ ` ` ` `bestWindow = wR-wL; ` ` ` `bestL = wL; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print positions of zeroes in the widest window ` ` ` `for` `(` `int` `i=` `0` `; i<bestWindow; i++) ` ` ` `{ ` ` ` `if` `(arr[bestL+i] == ` `0` `) ` ` ` `System.out.print(bestL+i + ` `" "` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver method to test the above function ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `m = ` `2` `; ` ` ` `System.out.println(` `"Indexes of zeroes to be flipped are "` `); ` ` ` ` ` `findZeroes(m); ` ` ` `} ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find positions ` `# of zeroes flipping which produces ` `# maximum number of xonsecutive 1's ` ` ` `# m is maximum of number zeroes allowed ` `# to flip, n is size of array ` `def` `findZeroes(arr, n, m) : ` ` ` ` ` `# Left and right indexes of current window ` ` ` `wL ` `=` `wR ` `=` `0` ` ` ` ` `# Left index and size of the widest window ` ` ` `bestL ` `=` `bestWindow ` `=` `0` ` ` ` ` `# Count of zeroes in current window ` ` ` `zeroCount ` `=` `0` ` ` ` ` `# While right boundary of current ` ` ` `# window doesn't cross right end ` ` ` `while` `wR < n: ` ` ` ` ` `# If zero count of current window is less than m, ` ` ` `# widen the window toward right ` ` ` `if` `zeroCount <` `=` `m : ` ` ` `if` `arr[wR] ` `=` `=` `0` `: ` ` ` `zeroCount ` `+` `=` `1` ` ` `wR ` `+` `=` `1` ` ` ` ` `# If zero count of current window is more than m, ` ` ` `# reduce the window from left ` ` ` `if` `zeroCount > m : ` ` ` `if` `arr[wL] ` `=` `=` `0` `: ` ` ` `zeroCount ` `-` `=` `1` ` ` `wL ` `+` `=` `1` ` ` ` ` `# Updqate widest window if ` ` ` `# this window size is more ` ` ` `if` `(wR` `-` `wL > bestWindow) ` `and` `(zeroCount<` `=` `m) : ` ` ` `bestWindow ` `=` `wR ` `-` `wL ` ` ` `bestL ` `=` `wL ` ` ` ` ` `# Print positions of zeroes ` ` ` `# in the widest window ` ` ` `for` `i ` `in` `range` `(` `0` `, bestWindow): ` ` ` `if` `arr[bestL ` `+` `i] ` `=` `=` `0` `: ` ` ` `print` `(bestL ` `+` `i, end ` `=` `" "` `) ` ` ` `# Driver program ` `arr ` `=` `[` `1` `, ` `0` `, ` `0` `, ` `1` `, ` `1` `, ` `0` `, ` `1` `, ` `0` `, ` `1` `, ` `1` `] ` `m ` `=` `2` `n ` `=` `len` `(arr) ` `print` `(` `"Indexes of zeroes to be flipped are"` `, end ` `=` `" "` `) ` `findZeroes(arr, n, m) ` ` ` `# This code is contributed by Shreyanshi Arun. ` |

*chevron_right*

*filter_none*

## C#

`// C# to find positions of zeroes flipping which ` `// produces maximum number of consecutive 1's ` `using` `System; ` ` ` `class` `Test ` `{ ` ` ` `static` `int` `[]arr = ` `new` `int` `[]{1, 0, 0, 1, 1, ` ` ` `0, 1, 0, 1, 1}; ` ` ` ` ` `// m is maximum of number zeroes allowed to flip ` ` ` `static` `void` `findZeroes(` `int` `m) ` ` ` `{ ` ` ` `// Left and right indexes of current window ` ` ` `int` `wL = 0, wR = 0; ` ` ` ` ` `// Left index and size of the widest window ` ` ` `int` `bestL = 0, bestWindow = 0; ` ` ` ` ` `// Count of zeroes in current window ` ` ` `int` `zeroCount = 0; ` ` ` ` ` `// While right boundary of current ` ` ` `// window doesn't cross right end ` ` ` `while` `(wR < arr.Length) ` ` ` `{ ` ` ` `// If zero count of current window is less ` ` ` `// than m, widen the window toward right ` ` ` `if` `(zeroCount <= m) ` ` ` `{ ` ` ` `if` `(arr[wR] == 0) ` ` ` `zeroCount++; ` ` ` `wR++; ` ` ` `} ` ` ` ` ` `// If zero count of current window is more than m, ` ` ` `// reduce the window from left ` ` ` `if` `(zeroCount > m) ` ` ` `{ ` ` ` `if` `(arr[wL] == 0) ` ` ` `zeroCount--; ` ` ` `wL++; ` ` ` `} ` ` ` ` ` `// Update widest window if this window size is more ` ` ` `if` `((wR-wL > bestWindow) && (zeroCount<=m)) ` ` ` `{ ` ` ` `bestWindow = wR-wL; ` ` ` `bestL = wL; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print positions of zeroes in the widest window ` ` ` `for` `(` `int` `i = 0; i < bestWindow; i++) ` ` ` `{ ` ` ` `if` `(arr[bestL + i] == 0) ` ` ` `Console.Write(bestL + i + ` `" "` `); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Driver method to test the above function ` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{ ` ` ` `int` `m = 2; ` ` ` `Console.Write(` `"Indexes of zeroes to be flipped are "` `); ` ` ` `findZeroes(m); ` ` ` `} ` `} ` ` ` `// This code is contributed by parashar ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find positions of ` `// zeroes flipping which produces ` `// maximum number of xonsecutive 1's ` ` ` `// m is maximum of number zeroes ` `// allowed to flip n is size of array ` `function` `findZeroes(` `$arr` `, ` `$n` `, ` `$m` `) ` `{ ` ` ` ` ` `// Left and right indexes ` ` ` `// of current window ` ` ` `$wL` `= 0; ` ` ` `$wR` `= 0; ` ` ` ` ` `// Left index and size of ` ` ` `// the widest window ` ` ` `$bestL` `= 0; ` ` ` `$bestWindow` `= 0; ` ` ` ` ` `// Count of zeroes in ` ` ` `// current window ` ` ` `$zeroCount` `= 0; ` ` ` ` ` `// While right boundary of ` ` ` `// current window doesn't cross ` ` ` `// right end ` ` ` `while` `(` `$wR` `< ` `$n` `) ` ` ` `{ ` ` ` ` ` `// If zero count of current ` ` ` `// window is less than m, ` ` ` `// widen the window toward right ` ` ` `if` `(` `$zeroCount` `<= ` `$m` `) ` ` ` `{ ` ` ` `if` `(` `$arr` `[` `$wR` `] == 0) ` ` ` `$zeroCount` `++; ` ` ` `$wR` `++; ` ` ` `} ` ` ` ` ` `// If zero count of current ` ` ` `// window is more than m, ` ` ` `// reduce the window from left ` ` ` `if` `(` `$zeroCount` `> ` `$m` `) ` ` ` `{ ` ` ` `if` `(` `$arr` `[` `$wL` `] == 0) ` ` ` `$zeroCount` `--; ` ` ` `$wL` `++; ` ` ` `} ` ` ` ` ` `// Updqate widest window if ` ` ` `// this window size is more ` ` ` `if` `((` `$wR` `-` `$wL` `> ` `$bestWindow` `) && (` `$zeroCount` `<=` `$m` `)) ` ` ` `{ ` ` ` `$bestWindow` `= ` `$wR` `- ` `$wL` `; ` ` ` `$bestL` `= ` `$wL` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Print positions of zeroes ` ` ` `// in the widest window ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$bestWindow` `; ` `$i` `++) ` ` ` `{ ` ` ` `if` `(` `$arr` `[` `$bestL` `+ ` `$i` `] == 0) ` ` ` `echo` `$bestL` `+ ` `$i` `. ` `" "` `; ` ` ` `} ` `} ` ` ` ` ` `// Driver Code ` ` ` `$arr` `= ` `array` `(1, 0, 0, 1, 1, 0, 1, 0, 1, 1); ` ` ` `$m` `= 2; ` ` ` `$n` `= sizeof(` `$arr` `)/sizeof(` `$arr` `[0]); ` ` ` `echo` `"Indexes of zeroes to be flipped are "` `; ` ` ` `findZeroes(` `$arr` `, ` `$n` `, ` `$m` `); ` ` ` `return` `0; ` ` ` `// This code is contributed by nitin mittal. ` `?> ` |

*chevron_right*

*filter_none*

**Output :**

Indexes of zeroes to be flipped are 5 7

This article is contributed by Ekta Goel. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Find the number of zeroes
- Smallest subsequence with sum of absolute difference of consecutive elements maximized
- Maximum number of 0s that can be flipped such that Array has no adjacent 1s
- Count number of bits to be flipped to convert A to B
- Find the number of consecutive zero at the end after multiplying n numbers
- Find ratio of zeroes, positive numbers and negative numbers in the Array
- Minimum cells to be flipped to get a 2*2 submatrix with equal elements
- Find minimum sum such that one of every three consecutive elements is taken
- Find all ranges of consecutive numbers from Array
- Subsequence X of length K such that gcd(X[0], X[1]) + (X[2], X[3]) + ... is maximized
- Find missing element in a sorted array of consecutive numbers
- Count number of binary strings without consecutive 1's
- Find the string formed by joining k consecutive nodes of linked list
- Divide the array in K segments such that the sum of minimums is maximized
- Place k elements such that minimum distance is maximized
- Number of sub-sequence such that it has one consecutive element with difference less than or equal to 1
- Number of ways an array can be filled with 0s and 1s such that no consecutive elements are 1
- Generate an Array such with elements maximized through swapping bits
- Pick points from array such that minimum distance is maximized
- Maximum number of consecutive 1's in binary representation of all the array elements