给定一个二进制数组arr ,任务是找到可以翻转的最大 0 数,使得该数组没有相邻的 1,即该数组在连续索引处不包含任何两个 1。
例子:
Input: arr[] = {1, 0, 0, 0, 1}
Output: 1
Explanation:
The 0 at index 2 can be replaced by 1.
Input: arr[] = {1, 0, 0, 1}
Output: 0
Explanation:
No 0 (zeroes) can be replaced by 1 such that no two consecutive indices have 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
Javascript
输出:
1
时间复杂度: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live