📌  相关文章
📜  可以翻转的最大0数,以使Array没有相邻的1

📅  最后修改于: 2021-05-17 20:23:40             🧑  作者: Mango

给定一个二进制数组arr ,任务是找到可以翻转的最大0数,以使该数组没有相邻的1,即该数组在连续索引处不包含任何两个1。

例子:

方法:

  • 遍历数组,对于每个索引为0的索引,检查其相邻的两个索引是否为0。对于数组的最后一个索引和第一个索引,分别检查相邻的左索引和右索引。
  • 对于满足上述条件的每个此类索引,请增加计数。
  • 在最后打印最终计数作为所需答案

下面的代码是上述方法的实现:

C++
// C++ program for the above approach 
#include
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____


Java
// 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));
    }
}


Python3
# 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


C#
// 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


输出:
1

时间复杂度: O(N)