Given a binary array arr[] of zero’s and one’s only. The task is to find the minimum number of one’s to be changed to zero such if there exist any index in the array such that arr[i] = 0 then arr[i-1] and arr[i+1] both should not be equals to at the same time.

That is, for any index the below condition should fail:

if (arr[i]== 0): (arr[i-1] == 1) && (arr[i+1] == 1)

**Note**: 1-based indexing is considered for the array.

**Examples**:

Input: arr[] = { 1, 1, 0, 1, 1, 0, 1, 0, 1, 0 }

Output: 2

Explanation: Indexs 2 and 7 OR 4 and 7 can be changed to zero.

Input: arr[] = { 1, 1, 0, 0, 0 }

Output: 0

**Approach:** The idea is that, whenever we found condition like we simply changed the value of (i+1)th index to zero(0). So that index between (i-1)-th and (i+1)-th index is safe.

Below is the implementation of the above approach:

## C++

`// C++ program to find minimum number ` `// of 1's to be replaced to 0's ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find minimum number ` `// of 1's to be replaced to 0's ` `int` `minChanges(` `int` `A[], ` `int` `n) ` `{ ` ` ` `int` `cnt = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n - 2; ++i) { ` ` ` ` ` `if` `((i - 1 >= 0) && A[i - 1] == 1 ` ` ` `&& A[i + 1] == 1 && A[i] == 0) { ` ` ` `A[i + 1] = 0; ` ` ` `cnt++; ` ` ` `} ` ` ` ` ` `} ` ` ` ` ` `// return final answer ` ` ` `return` `cnt; ` `} ` ` ` `// Driver program ` `int` `main() ` `{ ` ` ` `int` `A[] = { 1, 1, 0, 1, 1, 0, 1, 0, 1, 0 }; ` ` ` `int` `n = ` `sizeof` `(A) / ` `sizeof` `(A[0]); ` ` ` ` ` `cout << minChanges(A, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find minimum number ` `// of 1's to be replaced to 0's ` `import` `java.lang.*; ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` `// Function to find minimum number ` `// of 1's to be replaced to 0's ` `static` `int` `minChanges(` `int` `[] A, ` `int` `n) ` `{ ` ` ` `int` `cnt = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n - ` `2` `; ++i) ` ` ` `{ ` ` ` ` ` `if` `((i - ` `1` `>= ` `0` `) && A[i - ` `1` `] == ` `1` `&& ` ` ` `A[i + ` `1` `] == ` `1` `&& ` ` ` `A[i] == ` `0` `) ` ` ` `{ ` ` ` `A[i + ` `1` `] = ` `0` `; ` ` ` `cnt++; ` ` ` `} ` ` ` ` ` `} ` ` ` ` ` `// return final answer ` ` ` `return` `cnt; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` ` ` `int` `[] A = { ` `1` `, ` `1` `, ` `0` `, ` `1` `, ` `1` `, ` `0` `, ` `1` `, ` `0` `, ` `1` `, ` `0` `}; ` ` ` `int` `n = A.length; ` ` ` ` ` `System.out.print(minChanges(A, n)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Akanksha Rai ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to find minimum ` `# number of 1's to be replaced to 0's ` ` ` `# Function to find minimum number ` `# of 1's to be replaced to 0's ` `def` `minChanges(A, n): ` ` ` `cnt ` `=` `0` ` ` `for` `i ` `in` `range` `(n ` `-` `2` `): ` ` ` `if` `((i ` `-` `1` `>` `=` `0` `) ` `and` `A[i ` `-` `1` `] ` `=` `=` `1` `and` ` ` `A[i ` `+` `1` `] ` `=` `=` `1` `and` `A[i] ` `=` `=` `0` `): ` ` ` `A[i ` `+` `1` `] ` `=` `0` ` ` `cnt ` `=` `cnt ` `+` `1` ` ` ` ` `# return final answer ` ` ` `return` `cnt ` ` ` `# Driver Code ` `A ` `=` `[` `1` `, ` `1` `, ` `0` `, ` `1` `, ` `1` `, ` `0` `, ` `1` `, ` `0` `, ` `1` `, ` `0` `] ` `n ` `=` `len` `(A) ` `print` `(minChanges(A, n)) ` ` ` `# This code is contributed ` `# by Shashank_Sharma ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find minimum number ` `// of 1's to be replaced to 0's ` `using` `System; ` ` ` `class` `GFG ` `{ ` `// Function to find minimum number ` `// of 1's to be replaced to 0's ` `static` `int` `minChanges(` `int` `[] A, ` `int` `n) ` `{ ` ` ` `int` `cnt = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n - 2; ++i) ` ` ` `{ ` ` ` ` ` `if` `((i - 1 >= 0) && A[i - 1] == 1 && ` ` ` `A[i + 1] == 1 && A[i] == 0) ` ` ` `{ ` ` ` `A[i + 1] = 0; ` ` ` `cnt++; ` ` ` `} ` ` ` ` ` `} ` ` ` ` ` `// return final answer ` ` ` `return` `cnt; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `[] A = { 1, 1, 0, 1, 1, 0, 1, 0, 1, 0 }; ` ` ` `int` `n = A.Length; ` ` ` ` ` `Console.Write(minChanges(A, n)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Akanksha Rai ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find minimum number ` `// of 1's to be replaced to 0's ` ` ` `// Function to find minimum number ` `// of 1's to be replaced to 0's ` `function` `minChanges(` `$A` `, ` `$n` `) ` `{ ` ` ` `$cnt` `= 0; ` ` ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `- 2; ++` `$i` `) ` ` ` `{ ` ` ` `if` `((` `$i` `- 1 >= 0) && ` `$A` `[` `$i` `- 1] == 1 && ` ` ` `$A` `[` `$i` `+ 1] == 1 && ` `$A` `[` `$i` `] == 0) ` ` ` `{ ` ` ` `$A` `[` `$i` `+ 1] = 0; ` ` ` `$cnt` `++; ` ` ` `} ` ` ` ` ` `} ` ` ` ` ` `// return final answer ` ` ` `return` `$cnt` `; ` `} ` ` ` `// Driver Code ` `$A` `= ` `array` `(1, 1, 0, 1, 1, ` ` ` `0, 1, 0, 1, 0); ` `$n` `= sizeof(` `$A` `); ` ` ` `echo` `minChanges(` `$A` `, ` `$n` `); ` ` ` `// This code is contributed ` `// by Ankita_Saini ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

2

**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.

## Recommended Posts:

- Minimum number of elements to be replaced to make the given array a Fibonacci Sequence
- Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array
- Find Index of 0 to be replaced with 1 to get longest continuous sequence of 1s in a binary array | Set-2
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Minimum characters to be replaced to remove the given substring
- Minimum characters to be replaced to make frequency of all characters same
- Modify the string such that every character gets replaced with the next character in the keyboard
- Minimum number of moves to make a binary array K periodic
- Minimum sub-array such that number of 1's in concatenation of binary representation of its elements is at least K
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
- Minimum number of Binary strings to represent a Number
- Minimum number of distinct powers of 2 required to express a given binary number
- Maximum and minimum of an array using minimum number of comparisons
- Find the occurrence of the given binary pattern in the binary representation of the array elements
- Insert minimum number in array so that sum of array becomes prime
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Minimum number of operations to convert array A to array B by adding an integer into a subarray
- Minimum flips in two binary arrays so that their XOR is equal to another array
- Minimum toggles to partition a binary array so that it has first 0s then 1s
- Minimum adjacent swaps required to Sort Binary array

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.