Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array | Set-2
Last Updated :
26 Aug, 2022
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).
Examples:
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 have discussed a solution for this problem in previous post. In this post two more methods to solve the problem are discussed.
Method 1(Using count of ones on both sides of zero):
The idea is to count number of ones on both sides of each zero. The required index is the index of zero having maximum number of ones around it. Following variables are used in implementation:
- leftCnt: To store count of ones on left side of current element zero under consideration.
- rightCnt: To store count of ones on right side of current element zero under consideration.
- maxIndex: Index of zero with maximum number of ones around it.
- lastInd: Index of last zero element seen
- maxCnt: Count of ones if zero at index maxInd is replaced by one.
Keep incrementing rightCnt until one is present in input array. Let next zero is present at index i. Check if this zero element is first zero element or not. It is first zero element if lastInd does not hold any valid index value. In that case update lastInd with i. Now the value of rightCnt is number of zeroes on left side of this zero. So leftCnt is equal to rightCnt and then again find value of rightCnt. If current zero element is not first zero, then number of ones around zero present at index lastInd is given by leftCnt + rightCnt. maxCnt will take value leftCnt + rightCnt + 1 and maxIndex = lastInd if this value is less than value currently held by maxCnt.
Now rightCnt will become leftCnt for zero at index i and lastInd will be equal to i. Again find value of rightCnt, compare number of ones with maxcnt and update maxCnt and maxIndex accordingly. Repeat this procedure for each subsequent zero element of the array. Observe that lastInd stores the index of zero for which current leftCnt and rightCnt are calculated. The required index of zero to be replaced with one is stored in maxIndex.
Following is the implementation of the above algorithm.
C++
#include <bits/stdc++.h>
using namespace std;
int maxOnesIndex( bool arr[], int n)
{
int i = 0;
int leftCnt = 0;
int rightCnt = 0;
int maxIndex = -1;
int lastInd = -1;
int maxCnt = 0;
while (i < n) {
if (arr[i]) {
rightCnt++;
}
else {
if (lastInd != -1) {
if (rightCnt + leftCnt + 1 > maxCnt) {
maxCnt = leftCnt + rightCnt + 1;
maxIndex = lastInd;
}
}
lastInd = i;
leftCnt = rightCnt;
rightCnt = 0;
}
i++;
}
if (lastInd != -1) {
if (leftCnt + rightCnt + 1 > maxCnt) {
maxCnt = leftCnt + rightCnt + 1;
maxIndex = lastInd;
}
}
return maxIndex;
}
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
class GFG {
static int maxOnesIndex( boolean arr[], int n) {
int i = 0 ;
int leftCnt = 0 ;
int rightCnt = 0 ;
int maxIndex = - 1 ;
int lastInd = - 1 ;
int maxCnt = 0 ;
while (i < n) {
if (arr[i]) {
rightCnt++;
} else {
if (lastInd != - 1 ) {
if (rightCnt + leftCnt + 1 > maxCnt) {
maxCnt = leftCnt + rightCnt + 1 ;
maxIndex = lastInd;
}
}
lastInd = i;
leftCnt = rightCnt;
rightCnt = 0 ;
}
i++;
}
if (lastInd != - 1 ) {
if (leftCnt + rightCnt + 1 > maxCnt) {
maxCnt = leftCnt + rightCnt + 1 ;
maxIndex = lastInd;
}
}
return maxIndex;
}
public static void main(String[] args) {
boolean arr[] = { true , true , false , false , true ,
false , true , true , true , false , true , true , true ,};
int n = arr.length;
System.out.println( "Index of 0 to be replaced is "
+ maxOnesIndex(arr, n));
}
}
|
Python3
def maxOnesIndex(arr, n):
i = 0
leftCnt = 0
rightCnt = 0
maxIndex = - 1
lastInd = - 1
maxCnt = 0
while i < n:
if arr[i] = = 1 :
rightCnt + = 1
else :
if lastInd ! = - 1 :
if rightCnt + leftCnt + 1 > maxCnt:
maxCnt = leftCnt + rightCnt + 1
maxIndex = lastInd
lastInd = i
leftCnt = rightCnt
rightCnt = 0
i + = 1
if lastInd ! = - 1 :
if leftCnt + rightCnt + 1 > maxCnt:
maxCnt = leftCnt + rightCnt + 1
maxIndex = lastInd
return maxIndex
if __name__ = = "__main__" :
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;
public class GFG{
static int maxOnesIndex( bool []arr, int n) {
int i = 0;
int leftCnt = 0;
int rightCnt = 0;
int maxIndex = -1;
int lastInd = -1;
int maxCnt = 0;
while (i < n) {
if (arr[i]) {
rightCnt++;
} else {
if (lastInd != -1) {
if (rightCnt + leftCnt + 1 > maxCnt) {
maxCnt = leftCnt + rightCnt + 1;
maxIndex = lastInd;
}
}
lastInd = i;
leftCnt = rightCnt;
rightCnt = 0;
}
i++;
}
if (lastInd != -1) {
if (leftCnt + rightCnt + 1 > maxCnt) {
maxCnt = leftCnt + rightCnt + 1;
maxIndex = lastInd;
}
}
return maxIndex;
}
static public void Main (){
bool []arr = { true , true , false , false , true ,
false , true , true , true , false , true , true , true ,};
int n = arr.Length;
Console.WriteLine( "Index of 0 to be replaced is "
+ maxOnesIndex(arr, n));
}
}
|
PHP
<?php
function maxOnesIndex( $arr , $n )
{
$i = 0;
$leftCnt = 0;
$rightCnt = 0;
$maxIndex = -1;
$lastInd = -1;
$maxCnt = 0;
while ( $i < $n )
{
if ( $arr [ $i ])
{
$rightCnt ++;
}
else
{
if ( $lastInd != -1)
{
if ( $rightCnt + $leftCnt + 1 > $maxCnt )
{
$maxCnt = $leftCnt + $rightCnt + 1;
$maxIndex = $lastInd ;
}
}
$lastInd = $i ;
$leftCnt = $rightCnt ;
$rightCnt = 0;
}
$i ++;
}
if ( $lastInd != -1)
{
if ( $leftCnt + $rightCnt + 1 > $maxCnt )
{
$maxCnt = $leftCnt + $rightCnt + 1;
$maxIndex = $lastInd ;
}
}
return $maxIndex ;
}
$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)
{
var i = 0;
var leftCnt = 0;
var rightCnt = 0;
var maxIndex = -1;
var lastInd = -1;
var maxCnt = 0;
while (i < n) {
if (arr[i]) {
rightCnt++;
}
else {
if (lastInd != -1) {
if (rightCnt + leftCnt + 1 > maxCnt) {
maxCnt = leftCnt + rightCnt + 1;
maxIndex = lastInd;
}
}
lastInd = i;
leftCnt = rightCnt;
rightCnt = 0;
}
i++;
}
if (lastInd != -1) {
if (leftCnt + rightCnt + 1 > maxCnt) {
maxCnt = leftCnt + rightCnt + 1;
maxIndex = lastInd;
}
}
return maxIndex;
}
var arr = [ 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1 ];
var 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
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Method 2(Using sliding window):
A sliding window is used to find number of ones in longest continuous sequence obtained by replacing a zero. The idea is to keep incrementing ending point of sliding window until one is present in input array. When a zero is found, check if it is first zero element or not. If it is first zero element then the sliding window is expanded further. If it is not, then find the length of sliding window.
This length is number of ones obtained by replacing the zero element present in sliding window. Note that this length gives number of ones obtained by replacing previous zero element and not current zero element. For current zero element, the starting point of sliding window is index next to the index of previous zero element.
Following is the implementation of the above algorithm.
C++
#include <bits/stdc++.h>
using namespace std;
int maxOnesIndex( bool arr[], int n)
{
int start = 0;
int end = 0;
int maxIndex = -1;
int lastInd = -1;
int maxCnt = 0;
while (end < n) {
while (end < n && arr[end]) {
end++;
}
if (maxCnt < end - start && lastInd != -1) {
maxCnt = end - start;
maxIndex = lastInd;
}
start = lastInd + 1;
lastInd = end;
end++;
}
if (maxCnt < end - start && lastInd != -1) {
maxCnt = end - start;
maxIndex = lastInd;
}
return maxIndex;
}
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
public class GFG {
static int maxOnesIndex( boolean arr[], int n) {
int start = 0 ;
int end = 0 ;
int maxIndex = - 1 ;
int lastInd = - 1 ;
int maxCnt = 0 ;
while (end < n) {
while (end < n && arr[end]) {
end++;
}
if (maxCnt < end - start && lastInd != - 1 ) {
maxCnt = end - start;
maxIndex = lastInd;
}
start = lastInd + 1 ;
lastInd = end;
end++;
}
if (maxCnt < end - start && lastInd != - 1 ) {
maxCnt = end - start;
maxIndex = lastInd;
}
return maxIndex;
}
static public void main(String[] args) {
boolean arr[] = { true , true , false , false , true ,
false , true , true , true , false , true , true , true ,};
int n = arr.length;
System.out.println( "Index of 0 to be replaced is "
+ maxOnesIndex(arr, n));
}
}
|
Python3
def maxOnesIndex(arr, n):
start = 0
end = 0
maxIndex = - 1
lastInd = - 1
maxCnt = 0
while (end < n) :
while (end < n and arr[end]) :
end + = 1
if (maxCnt < end - start and lastInd ! = - 1 ) :
maxCnt = end - start
maxIndex = lastInd
start = lastInd + 1
lastInd = end
end + = 1
if (maxCnt < end - start and lastInd ! = - 1 ) :
maxCnt = end - start
maxIndex = lastInd
return maxIndex
if __name__ = = "__main__" :
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;
public class GFG
{
public static int maxOnesIndex( bool [] arr, int n)
{
int start = 0;
int end = 0;
int maxIndex = -1;
int lastInd = -1;
int maxCnt = 0;
while (end < n)
{
while (end < n && arr[end])
{
end++;
}
if (maxCnt < end - start && lastInd != -1)
{
maxCnt = end - start;
maxIndex = lastInd;
}
start = lastInd + 1;
lastInd = end;
end++;
}
if (maxCnt < end - start && lastInd != -1)
{
maxCnt = end - start;
maxIndex = lastInd;
}
return maxIndex;
}
public static void Main( string [] args)
{
bool [] arr = new bool [] { true , true , false , false , true , false , true , true , true , false , true , true , true };
int n = arr.Length;
Console.WriteLine( "Index of 0 to be replaced is " + maxOnesIndex(arr, n));
}
}
|
PHP
<?php
function maxOnesIndex( $arr , $n )
{
$start = 0;
$end = 0;
$maxIndex = -1;
$lastInd = -1;
$maxCnt = 0;
while ( $end < $n )
{
while ( $end < $n && $arr [ $end ])
{
$end ++;
}
if ( $maxCnt < $end - $start &&
$lastInd != -1)
{
$maxCnt = $end - $start ;
$maxIndex = $lastInd ;
}
$start = $lastInd + 1;
$lastInd = $end ;
$end ++;
}
if ( $maxCnt < $end - $start && $lastInd != -1)
{
$maxCnt = $end - $start ;
$maxIndex = $lastInd ;
}
return $maxIndex ;
}
$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)
{
var start = 0;
var end = 0;
var maxIndex = -1;
var lastInd = -1;
var maxCnt = 0;
while (end < n) {
while (end < n && arr[end]) {
end++;
}
if (maxCnt < end - start && lastInd != -1) {
maxCnt = end - start;
maxIndex = lastInd;
}
start = lastInd + 1;
lastInd = end;
end++;
}
if (maxCnt < end - start && lastInd != -1) {
maxCnt = end - start;
maxIndex = lastInd;
}
return maxIndex;
}
var arr = [ 1, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1 ];
var 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
Complexity Analysis:
- Time Complexity: O(n)
- Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...