Related Articles
Maximum flips possible such that no pair of adjacent elements are both 1
• Last Updated : 23 Apr, 2021

Given a binary array arr[] of size N, the task is to find the maximum count of 0s that can be converted into 1s such that no pair of adjacent array elements are 1.

Examples:

Input: arr[] = { 1, 0, 0, 0, 1 }
Output:
Explanation:
Updating arr to 1 modifies arr[] to { 1, 0, 1, 0, 1 }
Therefore, the required output is 1

Input: arr[] = { 0, 0, 1, 0, 0, 0, 0, 1, 0, 0 }
Output:
Explanation:
Updating arr, arr and arr modifies arr[] to { 1, 0, 1, 0, 0, 1, 0, 1, 0, 1 }
Therefore, the required output is 3

Approach: The problem can be solved using Greedy technique. Follow the steps below to solve the problem:

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to find the maximum count of 0s``// required to be converted into 1s such``// no pair of adjacent elements are 1``void` `maxPositionsOccupied(vector<``int``>& arr)``{` `    ``// Base Case``    ``if` `(arr.size() == 0) {``        ``cout << 0;``        ``return``;``    ``}` `    ``// Insert 0 at the end``    ``// of the array``    ``arr.push_back(0);` `    ``// Insert 0 at the front``    ``// of the array``    ``arr.insert(arr.begin(), 0);` `    ``// Stores the maximum count of of 0s``    ``// that can be converted into 1s``    ``int` `ans = 0;` `    ``// Stores index of array elements``    ``int` `i = 0;` `    ``// Traverse the array``    ``while` `((i < arr.size() - 2)) {` `        ``// If adjacent elements are 0s``        ``if` `((arr[i] == 0) && (arr[i + 1] == 0)``            ``&& (arr[i + 2] == 0)) {` `            ``// Update ans``            ``ans++;` `            ``// Update arr[i + 1]``            ``arr[i + 1] = 1;``        ``}` `        ``// Update i``        ``i++;``    ``}` `    ``// Print the answer``    ``cout << ans;``}` `// Driver Code``int` `main()``{``    ``// Given binary array``    ``vector<``int``> arr = { 1, 0, 0, 0, 1 };` `    ``// Prints the maximum 0 to 1``    ``// conversions required``    ``maxPositionsOccupied(arr);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``public` `class` `GFG``{` `  ``// Function to find the maximum count of 0s``  ``// required to be converted into 1s such``  ``// no pair of adjacent elements are 1``  ``static` `void` `maxPositionsOccupied(``int``[] arr)``  ``{` `    ``// Base Case``    ``if` `(arr.length == ``0``)``    ``{``      ``System.out.print(``0``);``      ``return``;``    ``}` `    ``// Stores the maximum count of of 0s``    ``// that can be converted into 1s``    ``int` `ans = ``0``;` `    ``// Stores index of array elements``    ``int` `i = ``0``;` `    ``// Traverse the array``    ``while` `((i < arr.length - ``2``))``    ``{` `      ``// If adjacent elements are 0s``      ``if` `((arr[i] == ``0``) &&``          ``(arr[i + ``1``] == ``0``) &&``          ``(arr[i + ``2``] == ``0``))``      ``{` `        ``// Update ans``        ``ans++;` `        ``// Update arr[i + 1]``        ``arr[i + ``1``] = ``1``;``      ``}` `      ``// Update i``      ``i++;``    ``}` `    ``// Print the answer``    ``System.out.print(ans);``  ``}` `  ``// Driver code``  ``public` `static` `void` `main(String[] args)``  ``{` `    ``// Given binary array``    ``int``[] arr = { ``1``, ``0``, ``0``, ``0``, ``1` `};` `    ``// Prints the maximum 0 to 1``    ``// conversions required``    ``maxPositionsOccupied(arr);``  ``}``}` `// This code is contributed by divyeshrabadiya07.`

## Python3

 `# Python3 program for the above approach` `# Function to find the maximum count of 0s``# required to be converted into 1s such``# no pair of adjacent elements are 1``def` `maxPositionsOccupied(arr):``    ` `    ``# Base Case``    ``if` `(``len``(arr) ``=``=` `0``):``        ``print``(``0``)` `    ``# Insert 0 at the end``    ``# of the array``    ``arr.append(``0``)` `    ``# Insert 0 at the front``    ``# of the array``    ``arr.insert(``0``, ``0``)` `    ``# Stores the maximum count of of 0s``    ``# that can be converted into 1s``    ``ans ``=` `0` `    ``# Stores index of array elements``    ``i ``=` `0` `    ``# Traverse the array``    ``while``((i < ``len``(arr) ``-` `2``)):``        ` `        ``# If adjacent elements are 0s``        ``if` `((arr[i] ``=``=` `0``) ``and``            ``(arr[i ``+` `1``] ``=``=` `0``) ``and``            ``(arr[i ``+` `2``] ``=``=` `0``)):` `            ``# Update ans``            ``ans ``+``=` `1` `            ``# Update arr[i + 1]``            ``arr[i ``+` `1``] ``=` `1` `        ``# Update i``        ``i ``+``=` `1` `    ``# Print the answer``    ``print``(ans)` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``# Given binary array``    ``arr ``=`  `[ ``1``, ``0``, ``0``, ``0``, ``1` `]` `    ``# Prints the maximum 0 to 1``    ``# conversions required``    ``maxPositionsOccupied(arr)` `# This code is contributed by SURENDRA_GANGWAR`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{` `// Function to find the maximum count of 0s``// required to be converted into 1s such``// no pair of adjacent elements are 1``static` `void` `maxPositionsOccupied(``int``[] arr)``{``    ` `    ``// Base Case``    ``if` `(arr.Length == 0)``    ``{``        ``Console.Write(0);``        ``return``;``    ``}``    ` `    ``// Stores the maximum count of of 0s``    ``// that can be converted into 1s``    ``int` `ans = 0;``  ` `    ``// Stores index of array elements``    ``int` `i = 0;``  ` `    ``// Traverse the array``    ``while` `((i < arr.Length - 2))``    ``{``        ` `        ``// If adjacent elements are 0s``        ``if` `((arr[i] == 0) &&``            ``(arr[i + 1] == 0) &&``            ``(arr[i + 2] == 0))``        ``{``            ` `            ``// Update ans``            ``ans++;``            ` `            ``// Update arr[i + 1]``            ``arr[i + 1] = 1;``        ``}``        ` `        ``// Update i``        ``i++;``    ``}``    ` `    ``// Print the answer``    ``Console.Write(ans);``}  ` `// Driver code``static` `void` `Main()``{``    ` `    ``// Given binary array``    ``int``[] arr = { 1, 0, 0, 0, 1 };``    ` `    ``// Prints the maximum 0 to 1``    ``// conversions required``    ``maxPositionsOccupied(arr);``}``}` `// This code is contributed by divyesh072019`

## Javascript

 ``
Output:
`1`

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

My Personal Notes arrow_drop_up