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(n2).
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++
#include<iostream>
using namespace std;
int maxOnesIndex( bool arr[], int n)
{
int max_count = 0;
int max_index;
int prev_zero = -1;
int prev_prev_zero = -1;
for ( int curr=0; curr<n; ++curr)
{
if (arr[curr] == 0)
{
if (curr - prev_prev_zero > max_count)
{
max_count = curr - prev_prev_zero;
max_index = prev_zero;
}
prev_prev_zero = prev_zero;
prev_zero = curr;
}
}
if (n-prev_prev_zero > max_count)
max_index = prev_zero;
return max_index;
}
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;
}
|
Java
import java.io.*;
class Binary
{
static int maxOnesIndex( int arr[], int n)
{
int max_count = 0 ;
int max_index= 0 ;
int prev_zero = - 1 ;
int prev_prev_zero = - 1 ;
for ( int curr= 0 ; curr<n; ++curr)
{
if (arr[curr] == 0 )
{
if (curr - prev_prev_zero > max_count)
{
max_count = curr - prev_prev_zero;
max_index = prev_zero;
}
prev_prev_zero = prev_zero;
prev_zero = curr;
}
}
if (n-prev_prev_zero > max_count)
max_index = prev_zero;
return max_index;
}
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));
}
}
|
Python3
def maxOnesIndex(arr,n):
max_count = 0
max_index = 0
prev_zero = - 1
prev_prev_zero = - 1
for curr in range (n):
if (arr[curr] = = 0 ):
if (curr - prev_prev_zero > max_count):
max_count = curr - prev_prev_zero
max_index = prev_zero
prev_prev_zero = prev_zero
prev_zero = curr
if (n - prev_prev_zero > max_count):
max_index = prev_zero
return max_index
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))
|
C#
using System;
class GFG {
static int maxOnesIndex( int []arr, int n)
{
int max_count = 0;
int max_index = 0;
int prev_zero = -1;
int prev_prev_zero = -1;
for ( int curr = 0; curr < n; ++curr)
{
if (arr[curr] == 0)
{
if (curr - prev_prev_zero > max_count)
{
max_count = curr - prev_prev_zero;
max_index = prev_zero;
}
prev_prev_zero = prev_zero;
prev_zero = curr;
}
}
if (n-prev_prev_zero > max_count)
max_index = prev_zero;
return max_index;
}
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));
}
}
|
PHP
<?php
function maxOnesIndex( $arr , $n )
{
$max_count = 0;
$max_index ;
$prev_zero = -1;
$prev_prev_zero = -1;
for ( $curr = 0; $curr < $n ; ++ $curr )
{
if ( $arr [ $curr ] == 0)
{
if ( $curr - $prev_prev_zero > $max_count )
{
$max_count = $curr - $prev_prev_zero ;
$max_index = $prev_zero ;
}
$prev_prev_zero = $prev_zero ;
$prev_zero = $curr ;
}
}
if ( $n - $prev_prev_zero > $max_count )
$max_index = $prev_zero ;
return $max_index ;
}
$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 );
?>
|
Javascript
<script>
function maxOnesIndex(arr, n)
{
let max_count = 0;
let max_index = 0;
let prev_zero = -1;
let prev_prev_zero = -1;
for (let curr = 0; curr < n; ++curr)
{
if (arr[curr] == 0)
{
if (curr - prev_prev_zero > max_count)
{
max_count = curr - prev_prev_zero;
max_index = prev_zero;
}
prev_prev_zero = prev_zero;
prev_zero = curr;
}
}
if (n - prev_prev_zero > max_count)
max_index = prev_zero;
return max_index;
}
let arr = [ 1, 1, 0, 0, 1, 0, 1,
1, 1, 0, 1, 1, 1 ];
let n = arr.length;
document.write( "Index of 0 to be replaced is " +
maxOnesIndex(arr, n));
</script>
|
Output
Index of 0 to be replaced is 9
Time Complexity: O(n)
Auxiliary Space: O(1)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
24 Jun, 2022
Like Article
Save Article