📌  相关文章
📜  翻转 K 长度子数组中的所有 0 后连续 1 的最大数量

📅  最后修改于: 2021-09-06 05:14:21             🧑  作者: Mango

给定一个长度为N的二进制数组arr[]和一个整数 K ,任务是在长度为 K 的子数组中翻转所有零后找到最大连续数组数。

例子:

解决方法:按照以下步骤解决问题:

  • 初始化其向左方向在正确的方向前进来迭代该阵列中从每个位置i(0至i-1)和从第(i + k以N-1)的可变假设TRAV。
  • 检查并保持一个帐户,在数组中迭代时,任何方向都没有零。
  • 如果有 0,则从该方向的循环中跳出。
  • 因此,最终对于i 到 i+k,如果有任何零,我们已经将其翻转为 1,因此无需计算此范围内的 1 的数量,因为它仅等于整数 K。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum number of
// consecutive 1's after flipping all
// zero in a K length subarray
int findmax(int arr[], int n, int k)
{
    // Initialize variable
    int trav, i;
    int c = 0, maximum = 0;
 
    // Iterate unil n-k+1 as we
    // have to go till i+k
    for (i = 0; i < n - k + 1; i++) {
        trav = i - 1;
        c = 0;
 
        /*Iterate in the array in left direction
        till you get 1 else break*/
        while (trav >= 0 && arr[trav] == 1) {
            trav--;
            c++;
        }
        trav = i + k;
 
        /*Iterate in the array in right direction
        till you get 1 else break*/
        while (trav < n && arr[trav] == 1) {
            trav++;
            c++;
        }
        c += k;
 
        // Compute the maximum length
        if (c > maximum)
            maximum = c;
    }
 
    // Return the length
    return maximum;
}
 
// Driver code
int main()
{
    int k = 3;
    // Array initialization
    int arr[] = { 0, 0, 1, 1, 0, 0, 0, 0 };
 
    // Size of array
    int n = sizeof arr / sizeof arr[0];
    int ans = findmax(arr, n, k);
    cout << ans << '\n';
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
     
// Function to find the maximum number of
// consecutive 1's after flipping all
// zero in a K length subarray
static int findmax(int arr[], int n, int k)
{
     
    // Initialize variable
    int trav, i;
    int c = 0, maximum = 0;
     
    // Iterate unil n-k+1 as we
    // have to go till i+k
    for(i = 0; i < n - k + 1; i++)
    {
        trav = i - 1;
        c = 0;
     
        // Iterate in the array in left direction
        // till you get 1 else break
        while (trav >= 0 && arr[trav] == 1)
        {
            trav--;
            c++;
        }
        trav = i + k;
     
        // Iterate in the array in right direction
        // till you get 1 else break
        while (trav < n && arr[trav] == 1)
        {
            trav++;
            c++;
        }
        c += k;
     
        // Compute the maximum length
        if (c > maximum)
            maximum = c;
    }
     
    // Return the length
    return maximum;
}
 
// Driver code
public static void main(String args[])
{
    int k = 3;
     
    // Array initialization
    int arr[] = { 0, 0, 1, 1, 0, 0, 0, 0 };
 
    // Size of array
    int n = arr.length;
    int ans = findmax(arr, n, k);
     
    System.out.println(ans);
}
}
 
// This code is contributed by Stream_Cipher


Python3
# Python3 program for the above approach
 
# Function to find the maximum number of
# consecutive 1's after flipping all
# zero in a K length subarray
def findmax(arr, n, k):
     
    # Initialize variable
    trav, i = 0, 0
    c = 0
    maximum = 0
 
    # Iterate unil n-k+1 as we
    # have to go till i+k
    while i < n - k + 1:
        trav = i - 1
        c = 0
 
        # Iterate in the array in left direction
        # till you get 1 else break
        while trav >= 0 and arr[trav] == 1:
            trav -= 1
            c += 1
        trav = i + k
 
        # Iterate in the array in right direction
        # till you get 1 else break
        while (trav < n and arr[trav] == 1):
            trav += 1
            c += 1
 
        c += k
 
        # Compute the maximum length
        if (c > maximum):
            maximum = c
        i += 1
 
    # Return the length
    return maximum
 
# Driver code
if __name__ == '__main__':
    k = 3
     
    # Array initialization
    arr = [0, 0, 1, 1, 0, 0, 0, 0]
 
    # Size of array
    n = len(arr)
    ans = findmax(arr, n, k)
    print(ans)
 
# This code is contributed by Mohit Kumar


C#
// C# program for the above approach
using System;
 
class GFG{
     
// Function to find the maximum number of
// consecutive 1's after flipping all
// zero in a K length subarray
static int findmax(int []arr, int n, int k)
{
     
    // Initialize variable
    int trav, i;
    int c = 0, maximum = 0;
     
    // Iterate unil n-k+1 as we
    // have to go till i+k
    for(i = 0; i < n - k + 1; i++)
    {
        trav = i - 1;
        c = 0;
     
        // Iterate in the array in left direction
        // till you get 1 else break
        while (trav >= 0 && arr[trav] == 1)
        {
            trav--;
            c++;
        }
        trav = i + k;
     
        // Iterate in the array in right direction
        // till you get 1 else break
        while (trav < n && arr[trav] == 1)
        {
            trav++;
            c++;
        }
        c += k;
     
        // Compute the maximum length
        if (c > maximum)
            maximum = c;
    }
     
    // Return the length
    return maximum;
}
 
// Driver code
public static void Main()
{
    int k = 3;
     
    // Array initialization
    int []arr = { 0, 0, 1, 1, 0, 0, 0, 0 };
 
    // Size of array
    int n = arr.Length;
    int ans = findmax(arr, n, k);
     
    Console.WriteLine(ans);
}
}
 
// This code is contributed by Stream_Cipher


输出
5

时间复杂度: O(N 2 )

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live