📜  二进制循环数组中的最大连续一个(或零)

📅  最后修改于: 2022-05-13 01:57:47.581000             🧑  作者: Mango

二进制循环数组中的最大连续一个(或零)

给定一个大小为 N 的二进制循环数组,任务是找到循环数组中出现的连续 1 的最大计数。
例子:

一个简单的解决方案是创建一个大小为 2*N 的数组,其中 N 是输入数组的大小。我们在这个数组中复制输入数组两次。现在我们需要在一次遍历中找到这个二进制数组中最长的连续 1。
有效解决方案:可以按照以下步骤解决上述问题:

  1. 我们可以使用取模运算符循环遍历数组,而不是创建一个大小为 2*N 的数组来实现循环数组。
  2. 从 0 迭代到 2*N 并找到连续的 1 数:
    • 从左到右遍历数组。
    • 每次遍历时,将当前索引计算为(i%N)以便在i>N时循环遍历数组。
    • 如果我们看到 1,我们增加计数并将其与迄今为止的最大值进行比较。如果我们看到 0,我们将计数重置为 0。
  3. 在某些情况下,当 a[i]==0 和 i>=n 时跳出循环以降低时间复杂度。

下面是上述方法的实现:

C++
// C++ program to count maximum consecutive
// 1's in a binary circular array
#include 
using namespace std;
 
// Function to return the count of maximum
// consecutive 1's in a binary circular array
int getMaxLength(bool arr[], int n)
{
 
    // Starting index
    int start = 0;
 
    // To store the maximum length of the
    // prefix of the given array with all 1s
    int preCnt = 0;
    while (start < n && arr[start] == 1) {
        preCnt++;
        start++;
    }
 
    // Ending index
    int end = n - 1;
 
    // To store the maximum length of the
    // suffix of the given array with all 1s
    int suffCnt = 0;
    while (end >= 0 && arr[end] == 1) {
        suffCnt++;
        end--;
    }
 
    // The array contains all 1s
    if (start > end)
        return n;
 
    // Find the maximum length subarray
    // with all 1s from the remaining not
    // yet traversed subarray
    int midCnt = 0;
 
    // To store the result for middle 1s
    int result = 0;
     
    for (int i = start; i <= end; i++) {
        if (arr[i] == 1) {
            midCnt++;
            result = max(result, midCnt);
        }
        else {
            midCnt = 0;
        }
    }
 
    // (preCnt + suffCnt) is the subarray when
    // the given array is assumed to be circular
    return max(result, preCnt + suffCnt);
}
 
// Driver code
int main()
{
    bool arr[] = { 1, 1, 0, 0, 1, 0, 1,
                   0, 1, 1, 1, 1 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    cout << getMaxLength(arr, n);
 
    return 0;
}


Java
// Java program to count maximum consecutive
// 1's in a binary circular array
class GfG {
 
    // Function to return the count of maximum
    // consecutive 1's in a binary circular array
    static int getMaxLength(int arr[], int n)
    {
        // Starting index
        int start = 0;
 
        // To store the maximum length of the
        // prefix of the given array with all 1s
        int preCnt = 0;
        while (start < n && arr[start] == 1) {
            preCnt++;
            start++;
        }
 
        // Ending index
        int end = n - 1;
 
        // To store the maximum length of the
        // suffix of the given array with all 1s
        int suffCnt = 0;
        while (end >= 0 && arr[end] == 1) {
            suffCnt++;
            end--;
        }
 
        // The array contains all 1s
        if (start > end)
            return n;
 
        // Find the maximum length subarray
        // with all 1s from the remaining not
        // yet traversed subarray
        int midCnt = 0;
 
        // To store the result for middle 1s
        int result = 0;
 
        for (int i = start; i <= end; i++) {
            if (arr[i] == 1) {
                midCnt++;
                result = Math.max(result, midCnt);
            }
            else {
                midCnt = 0;
            }
        }
 
        // (preCnt + suffCnt) is the subarray when
        // the given array is assumed to be circular
        return Math.max(result, preCnt + suffCnt);
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = new int[] { 1, 1, 0, 0, 1, 0,
                                1, 0, 1, 1, 1, 1 };
        int n = arr.length;
        System.out.println(getMaxLength(arr, n));
    }
}
 
// This code is contributed by Prerna Saini


Python3
# Python 3 program to count maximum consecutive
# 1's in a binary circular array
 
# Function to return the count of
# maximum consecutive 1's in a
# binary circular array
def getMaxLength(arr, n):
     
  
    # Starting index
    start = 0
     
    # To store the maximum length of the
    # prefix of the given array with all 1s
    preCnt = 0
    while(start < n and arr[start] == 1):
        preCnt = preCnt + 1
        start = start + 1
     
    # Ending index
    end = n - 1
     
    # To store the maximum length of the
    # suffix of the given array with all 1s
    suffCnt = 0
    while(end >= 0 and arr[end] == 1):
        suffCnt = suffCnt + 1
        end = end - 1
     
    # The array contains all 1s
    if(start > end):
        return n
     
    # Find the maximum length subarray
    # with all 1s from the remaining not
    # yet traversed subarray
    midCnt = 0
     
    i = start
 
    # To store the result for middle 1s
    result = 0
 
    while(i <= end):
        if(arr[i] == 1):
            midCnt = midCnt + 1
            result = max(result, midCnt)
        else:
            midCnt = 0
        i = i + 1
     
    # (preCnt + suffCnt) is the subarray when
    # the given array is assumed to be circular
    return max(result, preCnt + suffCnt)
 
# Driver code
if __name__ == '__main__':
    arr = [1, 1, 0, 0, 1, 0,
        1, 0, 1, 1, 1, 1]
    n = len(arr)
    print(getMaxLength(arr, n))
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# program to count maximum consecutive
// 1's in a binary circular array
using System;
 
class GFG {
 
    // Function to return the count of maximum
    // consecutive 1's in a binary circular array
    static int getMaxLength(int[] arr, int n)
    {
 
        // Starting index
        int start = 0;
 
        // To store the maximum length of the
        // prefix of the given array with all 1s
        int preCnt = 0;
        while (start < n && arr[start] == 1) {
            preCnt++;
            start++;
        }
 
        // Ending index
        int end = n - 1;
 
        // To store the maximum length of the
        // suffix of the given array with all 1s
        int suffCnt = 0;
        while (end >= 0 && arr[end] == 1) {
            suffCnt++;
            end--;
        }
 
        // The array contains all 1s
        if (start > end)
            return n;
 
        // Find the maximum length subarray
        // with all 1s from the remaining not
        // yet traversed subarray
        int midCnt = 0;
 
        // To store the result for middle 1s
        int result = 0;
 
        for (int i = start; i <= end; i++) {
            if (arr[i] == 1) {
                midCnt++;
                result = Math.Max(result, midCnt);
            }
            else {
                midCnt = 0;
            }
        }
 
        // (preCnt + suffCnt) is the subarray when
        // the given array is assumed to be circular
        return Math.Max(result, preCnt + suffCnt);
    }
 
    // Driver code
    public static void Main()
    {
        int[] arr = new int[] { 1, 1, 0, 0, 1, 0,
                                1, 0, 1, 1, 1, 0 };
        int n = arr.Length;
        Console.WriteLine(getMaxLength(arr, n));
    }
}
 
// This code is contributed by Code_Mech.


Javascript


输出
6