在给定的二进制数组中最大化要翻转的 0,使得两个 1 之间至少有 K 个 0
给定一个二进制数组arr[]和一个整数K ,任务是计算可以翻转为1 的0 的最大数量,使得两个1之间至少有K个0 。
例子:
Input: arr[] = {0, 0, 1, 0, 0, 0}, K = 1
Output: 2
Explanation: The 1st and the 5th index in the array arr[] can be flipped such that there is atleast 1 zero between any two 1’s. Therefore, the array after flipping is arr[] = {1, 0, 1, 0, 1, 0}.
Input: arr[] = {1, 0, 0, 0, 0, 0, 0, 0, 1, 0}, K = 2
Output: 1
Explanation: The 4th index in the above array is the only valid index that can be flipped
方法:给定的问题可以通过遍历数组并找到两个 1 之间的连续零的计数来解决。假设,两个1之间的0 的数量是X 。然后,可以观察到可以在其间翻转的0 的数量为(XK) / (K+1) 。因此,遍历数组并跟踪两个1之间的连续0 的数量,类似于此处讨论的算法,并将可以翻转的0 的计数添加到变量cnt中,这是所需的答案。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum number
// of 1s that can be placed in array arr[]
int maximumOnes(int arr[], int N, int K)
{
// Stores the count of 1's
int cnt = 0;
// Stores the last index of 1
int last = -(K + 1);
// Loop to iterate through the array
for (int i = 0; i < N; i++) {
// If the current element is 1
if (arr[i] == 1) {
// Check if there are sufficient
// 0's between consecutive 1's to
// insert more 1's between them
if (i - last - 1 >= 2 * (K - 1)) {
cnt += (i - last - 1 - K) / (K + 1);
}
// Update the index of last 1
last = i;
}
}
// Condition to include the segment of
// 0's in the last
cnt += (N - last - 1) / (K + 1);
// Return answer
return cnt;
}
// Driver Code
int main()
{
int arr[] = { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0 };
int N = sizeof(arr) / sizeof(arr[0]);
int K = 2;
cout << maximumOnes(arr, N, K);
return 0;
}
Java
// Java program for the above approach
import java.io.*;
class GFG
{
// Function to find the maximum number
// of 1s that can be placed in array arr[]
static int maximumOnes(int arr[], int N, int K)
{
// Stores the count of 1's
int cnt = 0;
// Stores the last index of 1
int last = -(K + 1);
// Loop to iterate through the array
for (int i = 0; i < N; i++) {
// If the current element is 1
if (arr[i] == 1) {
// Check if there are sufficient
// 0's between consecutive 1's to
// insert more 1's between them
if (i - last - 1 >= 2 * (K - 1)) {
cnt += (i - last - 1 - K) / (K + 1);
}
// Update the index of last 1
last = i;
}
}
// Condition to include the segment of
// 0's in the last
cnt += (N - last - 1) / (K + 1);
// Return answer
return cnt;
}
// Driver Code
public static void main(String[] args)
{
int arr[] = { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0 };
int N = arr.length;
int K = 2;
System.out.println(maximumOnes(arr, N, K));
}
}
// This code is contributed by Potta Lokesh
Python3
# Python3 program for the above approach
# Function to find the maximum number
# of 1s that can be placed in array arr[]
def maximumOnes(arr, N, K) :
# Stores the count of 1's
cnt = 0;
# Stores the last index of 1
last = -(K + 1);
# Loop to iterate through the array
for i in range(N) :
# If the current element is 1
if (arr[i] == 1) :
# Check if there are sufficient
# 0's between consecutive 1's to
# insert more 1's between them
if (i - last - 1 >= 2 * (K - 1)) :
cnt += (i - last - 1 - K) // (K + 1);
# Update the index of last 1
last = i;
# Condition to include the segment of
# 0's in the last
cnt += (N - last - 1) // (K + 1);
# Return answer
return cnt;
# Driver Code
if __name__ == "__main__" :
arr = [ 1, 0, 0, 0, 0, 0, 0, 0, 1, 0 ];
N = len(arr);
K = 2;
print(maximumOnes(arr, N, K));
# This code is contributed by AnkThon
C#
// C# program for the above approach
using System;
public class GFG
{
// Function to find the maximum number
// of 1s that can be placed in array arr[]
static int maximumOnes(int []arr, int N, int K)
{
// Stores the count of 1's
int cnt = 0;
// Stores the last index of 1
int last = -(K + 1);
// Loop to iterate through the array
for (int i = 0; i < N; i++) {
// If the current element is 1
if (arr[i] == 1) {
// Check if there are sufficient
// 0's between consecutive 1's to
// insert more 1's between them
if (i - last - 1 >= 2 * (K - 1)) {
cnt += (i - last - 1 - K) / (K + 1);
}
// Update the index of last 1
last = i;
}
}
// Condition to include the segment of
// 0's in the last
cnt += (N - last - 1) / (K + 1);
// Return answer
return cnt;
}
// Driver Code
public static void Main(string[] args)
{
int []arr = { 1, 0, 0, 0, 0, 0, 0, 0, 1, 0 };
int N = arr.Length;
int K = 2;
Console.WriteLine(maximumOnes(arr, N, K));
}
}
// This code is contributed by AnkThon
Javascript
1
时间复杂度: O(N)
辅助空间: O(1)