📜  二进制数组中要替换的最小 1 数

📅  最后修改于: 2021-10-26 05:41:05             🧑  作者: Mango

给定一个只有 0 和 1 的二进制数组 arr[]。任务是找到要更改为零的最小数量,例如是否存在任何索引i  在数组中使得 arr[i] = 0 那么 arr[i-1] 和 arr[i+1] 都不应该等于1  同时。
也就是说,对于任何索引i  以下条件应该失败:

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

注意:数组考虑基于 1 的索引。
例子

方法:这个想法是,每当我们发现条件像arr[i-1] = 1 \;and\; arr[i] = 0 \;and \;arr[i+1] = 1  我们只是将第 (i+1) 个索引的值更改为零 (0)。所以第 (i-1)-th 和 (i+1)-th 索引之间的索引是安全的。
下面是上述方法的实现:

C++
// C++ program to find minimum number
// of 1's  to be replaced to 0's
#include 
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
= 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


输出:
2

时间复杂度: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程