Maximum number of 0s that can be flipped such that Array has no adjacent 1s

Given a binary array arr, the task is to find the maximum number of 0s that can be flipped such that the array has no adjacent 1s, i.e. the array does not contain any two 1s at consecutive indices.

Examples:

Input: arr[] = {1, 0, 0, 0, 1}
Output: 1
Explanation:
The 0 at index 2 can be replaced by 1.

Input: arr[] = {1, 0, 0, 1}
Output: 0
Explanation:
No 0 (zeroes) can be replaced by 1 such that no two consecutive indices have 1.

Approach:



  • Iterate over the array and for every index which have 0, check if its adjacent two indices have 0 or not. For the last and first index of the array, check for the adjacent left and right index respectively.
  • For every such index satisfying the above condition, increase the count.
  • Print the final count at the end as the required answer

Below code is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program for the above approach 
#include<bits/stdc++.h>
using namespace std;
  
// Maximum number of 0s that 
// can be replaced by 1 
int canReplace(int array[], int n) 
    int i = 0, count = 0; 
  
    while (i < n)
    
          
        // Check for three consecutive 0s 
        if (array[i] == 0 && 
            (i == 0 || array[i - 1] == 0) &&
            (i == n - 1|| array[i + 1] == 0)) 
        
  
            // Flip the bit 
            array[i] = 1; 
  
            // Increase the count 
            count++; 
        
        i++; 
    
    return count; 
  
// Driver's Code 
int main()
    int array[5] = { 1, 0, 0, 0, 1 };     
      
    cout << canReplace(array, 5);
  
// This code is contributed by spp____ 

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program for the above approach
  
public class geeks {
  
    // Maximum number of 0s that
    // can be replaced by 1
    public static int canReplace(
        int[] array)
    {
        int i = 0, count = 0;
  
        while (i < array.length) {
  
            // Check for three consecutive 0s
            if (array[i] == 0
                && (i == 0
                    || array[i - 1] == 0)
                && (i == array.length - 1
                    || array[i + 1] == 0)) {
  
                // Flip the bit
                array[i] = 1;
  
                // Increase the count
                count++;
            }
            i++;
        }
  
        return count;
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int[] array = { 1, 0, 0, 0, 1 };
        System.out.println(canReplace(array));
    }
}

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program for the above approach
  
# Maximum number of 0s that 
# can be replaced by 1
def canReplace(arr, n):
  
    i = 0
    count = 0
  
    while (i < n):
  
        # Check for three consecutive 0s
        if (arr[i] == 0 and
                (i == 0 or arr[i - 1] == 0) and
                (i == n - 1 or arr[i + 1] == 0)):
  
            # Flip the bit
            arr[i] = 1
  
            # Increase the count
            count += 1
  
        i += 1
    return count
  
# Driver code
if __name__ == '__main__':
  
    arr = [ 1, 0, 0, 0, 1]
      
    print(canReplace(arr, 5))
  
# This code is contributed by himanshu77

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program for the above approach
using System;
  
class GFG{
  
// Maximum number of 0s that
// can be replaced by 1
public static int canReplace(int[] array)
{
    int i = 0, count = 0; 
    while (i < array.Length) 
    {
  
        // Check for three consecutive 0s
        if (array[i] == 0 && 
           (i == 0 || array[i - 1] == 0) && 
           (i == array.Length - 1 || array[i + 1] == 0)) 
        {
              
            // Flip the bit
            array[i] = 1;
  
            // Increase the count
            count++;
        }
        i++;
    }
  
    return count;
}
  
// Driver code
public static void Main(String []args)
{
    int[] array = { 1, 0, 0, 0, 1 };
      
    Console.WriteLine(canReplace(array));
}
}
  
// This code is contributed by Rajput-Ji

chevron_right


Output:

1

Time Complexity: O(N)

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : spp____, Rajput-Ji, himanshu77