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

📅  最后修改于: 2021-05-17 04:51:25             🧑  作者: 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)