Minimum number of 1's to be replaced in a binary array
Last Updated :
08 Sep, 2022
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 i in the array such that arr[i] = 0 then arr[i-1] and arr[i+1] both should not be equals to 1 at the same time.
That is, for any index i 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: Indexes 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 arr[i-1] = 1 \;and\; arr[i] = 0 \;and \;arr[i+1] = 1 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;
}
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
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
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
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
?>
JavaScript
<script>
// Javascript 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)
{
var cnt = 0;
for (var 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;
}
var A = [ 1, 1, 0, 1, 1, 0, 1, 0, 1, 0 ];
var n = A.length;
document.write( minChanges(A, n));
// This code is contributed by SoumikMondal
</script>
Complexity Analysis:
- Time Complexity: O(N)
- Auxiliary Space: O(1)
Similar Reads
Minimum number of moves to make a binary array K periodic Given a binary array arr[] (containing only 0s and 1s) and an integer K. The task is to find the minimum number of moves to make the array K-periodic. An array is said to be K-periodic if the sub-arrays [1 to K], [k+1 to 2K], [2k+1 to 3K], ... are all exactly same. In a single move any 1 can be chan
6 min read
Minimum number of characters to be removed to make a binary string alternate Given a binary string, the task is to find minimum number of characters to be removed from it so that it becomes alternate. A binary string is alternate if there are no two consecutive 0s or 1s.Examples : Input : s = "000111" Output : 4 We need to delete two 0s and two 1s to make string alternate. I
4 min read
Minimum number of steps required to place all 1s at a single index Given a binary array A[] of size N, where all 1s can be moved to its adjacent position, the task is to print an array res[] of size N, where res[i] contains the minimum number of steps required to move all the 1s at the ith index. Examples: Input: A[] = {1, 0, 1, 0}Output: {2, 2, 2, 4}Explanation: F
9 min read
Minimum number of Binary strings to represent a Number Given a number N. The task is to find the minimum number of binary strings required to represent the given number as the sum of the binary strings.Examples: Input : 131 Output : Minimum Number of binary strings needed: 3 111 10 10 Input : 564 Output :Minimum Number of binary strings needed: 6 111 11
7 min read
Minimum number of replacements to make the binary string alternating | Set 2 Given a binary string str, the task is to find the minimum number of characters in the string that have to be replaced in order to make the string alternating (i.e. of the form 01010101... or 10101010...).Examples: Input: str = "1100" Output: 2 Replace 2nd character with '0' and 3rd character with '
5 min read
Minimum length subarray of 1s in a Binary Array Given binary array. The task is to find the length of subarray with minimum number of 1s.Note: It is guaranteed that there is atleast one 1 present in the array.Examples : Input : arr[] = {1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1} Output : 3 Minimum length subarray of 1s is {1, 1}.Input : arr[] = {0, 0, 1
8 min read