Given an array of 0s and 1s, find the position of 0 to be replaced with 1 to get longest continuous sequence of 1s. Expected time complexity is O(n) and auxiliary space is O(1).

**Example:**

Input: arr[] = {1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1} Output: Index 9 Assuming array index starts from 0, replacing 0 with 1 at index 9 causes the maximum continuous sequence of 1s. Input: arr[] = {1, 1, 1, 1, 0} Output: Index 4

**We strongly recommend to minimize the browser and try this yourself first.**

A **Simple Solution** is to traverse the array, for every 0, count the number of 1s on both sides of it. Keep track of maximum count for any 0. Finally return index of the 0 with maximum number of 1s around it. The time complexity of this solution is O(n^{2}).

Using an **Efficient Solution**, the problem can solved in O(n) time. The idea is to keep track of three indexes, current index (*curr*), previous zero index (*prev_zero*) and previous to previous zero index (*prev_prev_zero*). Traverse the array, if current element is 0, calculate the difference between *curr *and *prev_prev_zero* (This difference minus one is the number of 1s around the prev_zero). If the difference between *curr *and *prev_prev_zero* is more than maximum so far, then update the maximum. Finally return index of the prev_zero with maximum difference.

Following are the implementations of the above algorithm.

## C++

`// C++ program to find Index of 0 to be replaced with 1 to get ` `// longest continuous sequence of 1s in a binary array ` `#include<iostream> ` `using` `namespace` `std; ` ` ` `// Returns index of 0 to be replaced with 1 to get longest ` `// continuous sequence of 1s. If there is no 0 in array, then ` `// it returns -1. ` `int` `maxOnesIndex(` `bool` `arr[], ` `int` `n) ` `{ ` ` ` `int` `max_count = 0; ` `// for maximum number of 1 around a zero ` ` ` `int` `max_index; ` `// for storing result ` ` ` `int` `prev_zero = -1; ` `// index of previous zero ` ` ` `int` `prev_prev_zero = -1; ` `// index of previous to previous zero ` ` ` ` ` `// Traverse the input array ` ` ` `for` `(` `int` `curr=0; curr<n; ++curr) ` ` ` `{ ` ` ` `// If current element is 0, then calculate the difference ` ` ` `// between curr and prev_prev_zero ` ` ` `if` `(arr[curr] == 0) ` ` ` `{ ` ` ` `// Update result if count of 1s around prev_zero is more ` ` ` `if` `(curr - prev_prev_zero > max_count) ` ` ` `{ ` ` ` `max_count = curr - prev_prev_zero; ` ` ` `max_index = prev_zero; ` ` ` `} ` ` ` ` ` `// Update for next iteration ` ` ` `prev_prev_zero = prev_zero; ` ` ` `prev_zero = curr; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Check for the last encountered zero ` ` ` `if` `(n-prev_prev_zero > max_count) ` ` ` `max_index = prev_zero; ` ` ` ` ` `return` `max_index; ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `bool` `arr[] = {1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1}; ` ` ` `int` `n = ` `sizeof` `(arr)/` `sizeof` `(arr[0]); ` ` ` `cout << ` `"Index of 0 to be replaced is "` ` ` `<< maxOnesIndex(arr, n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find Index of 0 to be replaced with 1 to get ` `// longest continuous sequence of 1s in a binary array ` ` ` `import` `java.io.*; ` ` ` `class` `Binary ` `{ ` ` ` `// Returns index of 0 to be replaced with 1 to get longest ` ` ` `// continuous sequence of 1s. If there is no 0 in array, then ` ` ` `// it returns -1. ` ` ` `static` `int` `maxOnesIndex(` `int` `arr[], ` `int` `n) ` ` ` `{ ` ` ` `int` `max_count = ` `0` `; ` `// for maximum number of 1 around a zero ` ` ` `int` `max_index=` `0` `; ` `// for storing result ` ` ` `int` `prev_zero = -` `1` `; ` `// index of previous zero ` ` ` `int` `prev_prev_zero = -` `1` `; ` `// index of previous to previous zero ` ` ` ` ` `// Traverse the input array ` ` ` `for` `(` `int` `curr=` `0` `; curr<n; ++curr) ` ` ` `{ ` ` ` `// If current element is 0, then calculate the difference ` ` ` `// between curr and prev_prev_zero ` ` ` `if` `(arr[curr] == ` `0` `) ` ` ` `{ ` ` ` `// Update result if count of 1s around prev_zero is more ` ` ` `if` `(curr - prev_prev_zero > max_count) ` ` ` `{ ` ` ` `max_count = curr - prev_prev_zero; ` ` ` `max_index = prev_zero; ` ` ` `} ` ` ` ` ` `// Update for next iteration ` ` ` `prev_prev_zero = prev_zero; ` ` ` `prev_zero = curr; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Check for the last encountered zero ` ` ` `if` `(n-prev_prev_zero > max_count) ` ` ` `max_index = prev_zero; ` ` ` ` ` `return` `max_index; ` ` ` `} ` ` ` ` ` ` ` `// Driver program to test above function ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `arr[] = {` `1` `, ` `1` `, ` `0` `, ` `0` `, ` `1` `, ` `0` `, ` `1` `, ` `1` `, ` `1` `, ` `0` `, ` `1` `, ` `1` `, ` `1` `}; ` ` ` `int` `n = arr.length; ` ` ` `System.out.println(` `"Index of 0 to be replaced is "` `+ ` ` ` `maxOnesIndex(arr, n)); ` ` ` `} ` `} ` `/* This code is contributed by Devesh Agrawal */` |

*chevron_right*

*filter_none*

## Python3

`# Python program to find Index ` `# of 0 to be replaced with 1 to get ` `# longest continuous sequence ` `# of 1s in a binary array ` ` ` `# Returns index of 0 to be ` `# replaced with 1 to get longest ` `# continuous sequence of 1s. ` `# If there is no 0 in array, then ` `# it returns -1. ` `def` `maxOnesIndex(arr,n): ` ` ` ` ` `# for maximum number of 1 around a zero ` ` ` `max_count ` `=` `0` ` ` ` ` `# for storing result ` ` ` `max_index ` `=` `0` ` ` ` ` `# index of previous zero ` ` ` `prev_zero ` `=` `-` `1` ` ` ` ` `# index of previous to previous zero ` ` ` `prev_prev_zero ` `=` `-` `1` ` ` ` ` `# Traverse the input array ` ` ` `for` `curr ` `in` `range` `(n): ` ` ` ` ` `# If current element is 0, ` ` ` `# then calculate the difference ` ` ` `# between curr and prev_prev_zero ` ` ` `if` `(arr[curr] ` `=` `=` `0` `): ` ` ` ` ` `# Update result if count of ` ` ` `# 1s around prev_zero is more ` ` ` `if` `(curr ` `-` `prev_prev_zero > max_count): ` ` ` ` ` `max_count ` `=` `curr ` `-` `prev_prev_zero ` ` ` `max_index ` `=` `prev_zero ` ` ` ` ` ` ` `# Update for next iteration ` ` ` `prev_prev_zero ` `=` `prev_zero ` ` ` `prev_zero ` `=` `curr ` ` ` ` ` `# Check for the last encountered zero ` ` ` `if` `(n` `-` `prev_prev_zero > max_count): ` ` ` `max_index ` `=` `prev_zero ` ` ` ` ` `return` `max_index ` ` ` `# Driver program ` ` ` `arr ` `=` `[` `1` `, ` `1` `, ` `0` `, ` `0` `, ` `1` `, ` `0` `, ` `1` `, ` `1` `, ` `1` `, ` `0` `, ` `1` `, ` `1` `, ` `1` `] ` `n ` `=` `len` `(arr) ` ` ` `print` `(` `"Index of 0 to be replaced is "` `, ` ` ` `maxOnesIndex(arr, n)) ` ` ` `# This code is contributed ` `# by Anant Agarwal. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find Index of 0 to be replaced ` `// with 1 to get longest continuous sequence of ` `// 1s in a binary array ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Returns index of 0 to be replaced with ` ` ` `// 1 to get longest continuous sequence of ` ` ` `// 1s. If there is no 0 in array, then it ` ` ` `// returns -1. ` ` ` `static` `int` `maxOnesIndex(` `int` `[]arr, ` `int` `n) ` ` ` `{ ` ` ` ` ` `// for maximum number of 1 around a zero ` ` ` `int` `max_count = 0; ` ` ` ` ` `// for storing result ` ` ` `int` `max_index = 0; ` ` ` ` ` `// index of previous zero ` ` ` `int` `prev_zero = -1; ` ` ` ` ` `// index of previous to previous zero ` ` ` `int` `prev_prev_zero = -1; ` ` ` ` ` `// Traverse the input array ` ` ` `for` `(` `int` `curr = 0; curr < n; ++curr) ` ` ` `{ ` ` ` ` ` `// If current element is 0, then ` ` ` `// calculate the difference ` ` ` `// between curr and prev_prev_zero ` ` ` `if` `(arr[curr] == 0) ` ` ` `{ ` ` ` ` ` `// Update result if count of 1s ` ` ` `// around prev_zero is more ` ` ` `if` `(curr - prev_prev_zero > max_count) ` ` ` `{ ` ` ` `max_count = curr - prev_prev_zero; ` ` ` `max_index = prev_zero; ` ` ` `} ` ` ` ` ` `// Update for next iteration ` ` ` `prev_prev_zero = prev_zero; ` ` ` `prev_zero = curr; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Check for the last encountered zero ` ` ` `if` `(n-prev_prev_zero > max_count) ` ` ` `max_index = prev_zero; ` ` ` ` ` `return` `max_index; ` ` ` `} ` ` ` ` ` `// Driver program to test above function ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `[]arr = {1, 1, 0, 0, 1, 0, 1, 1, 1, ` ` ` `0, 1, 1, 1}; ` ` ` `int` `n = arr.Length; ` ` ` `Console.Write(` `"Index of 0 to be replaced is "` ` ` `+ maxOnesIndex(arr, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by nitin mittal. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find Index of 0 to be ` `// replaced with 1 to get longest continuous ` `// sequence of 1s in a binary array ` ` ` `// Returns index of 0 to be replaced with ` `// 1 to get longest continuous sequence of 1s. ` `// If there is no 0 in array, then it returns -1. ` `function` `maxOnesIndex( ` `$arr` `, ` `$n` `) ` `{ ` ` ` `$max_count` `= 0; ` `// for maximum number of ` ` ` `// 1 around a zero ` ` ` `$max_index` `; ` `// for storing result ` ` ` `$prev_zero` `= -1; ` `// index of previous zero ` ` ` `$prev_prev_zero` `= -1; ` `// index of previous to ` ` ` `// previous zero ` ` ` ` ` `// Traverse the input array ` ` ` `for` `(` `$curr` `= 0; ` `$curr` `< ` `$n` `; ++` `$curr` `) ` ` ` `{ ` ` ` `// If current element is 0, then ` ` ` `// calculate the difference ` ` ` `// between curr and prev_prev_zero ` ` ` `if` `(` `$arr` `[` `$curr` `] == 0) ` ` ` `{ ` ` ` `// Update result if count of 1s ` ` ` `// around prev_zero is more ` ` ` `if` `(` `$curr` `- ` `$prev_prev_zero` `> ` `$max_count` `) ` ` ` `{ ` ` ` `$max_count` `= ` `$curr` `- ` `$prev_prev_zero` `; ` ` ` `$max_index` `= ` `$prev_zero` `; ` ` ` `} ` ` ` ` ` `// Update for next iteration ` ` ` `$prev_prev_zero` `= ` `$prev_zero` `; ` ` ` `$prev_zero` `= ` `$curr` `; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Check for the last encountered zero ` ` ` `if` `(` `$n` `- ` `$prev_prev_zero` `> ` `$max_count` `) ` ` ` `$max_index` `= ` `$prev_zero` `; ` ` ` ` ` `return` `$max_index` `; ` `} ` ` ` `// Driver Code ` `$arr` `= ` `array` `(1, 1, 0, 0, 1, 0, 1, ` ` ` `1, 1, 0, 1, 1, 1); ` `$n` `= sizeof(` `$arr` `); ` `echo` `"Index of 0 to be replaced is "` `, ` ` ` `maxOnesIndex(` `$arr` `, ` `$n` `); ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Index of 0 to be replaced is 9

**Time Complexity: **O(n)

**Auxiliary Space:** O(1)

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array | Set-2
- Longest alternating sub-array starting from every index in a Binary Array
- Minimum number of elements to be replaced to make the given array a Fibonacci Sequence
- Find initial sequence that produces a given Array by cyclic increments upto index P
- Minimum number of 1's to be replaced in a binary array
- Find original sequence from Array containing the sequence merged many times in order
- Longest sequence of positive integers in an array
- Longest sub-sequence of array containing Lucas numbers
- Longest increasing sequence by the boundary elements of an Array
- Longest increasing sub-sequence formed by concatenating array to itself N times
- Index of kth set bit in a binary array with update queries
- Find starting index for every occurrence of given array B in array A using Z-Algorithm
- How to find last index of a number in an Array in C++
- Find the index of first 1 in a sorted array of 0's and 1's
- Find minimum length sub-array which has given sub-sequence in it
- Find the index of first 1 in an infinite sorted array of 0s and 1s
- Find the occurrence of the given binary pattern in the binary representation of the array elements
- Find the final sequence of the array after performing given operations
- Find the index of the element in an array which divides most elements before it
- Find if a crest is present in the index range [L, R] of the given array