📌  相关文章
📜  二进制数组中连续 1 的第二长序列的长度

📅  最后修改于: 2021-09-08 12:37:26             🧑  作者: Mango

给定一个大小为N的二进制数组arr[] ,任务是找到数组中存在的第二长连续 1 序列的长度。

例子:

方法:这个想法是遍历给定的二进制数组并跟踪迄今为止遇到的连续数组的最大和第二大长度。以下是步骤:

  1. 初始化变量maxicountsecond_max分别存储连续 1 的最长、当前和第二长序列的长度。
  2. 迭代给定数组两次。
  3. 首先,从左到右遍历数组。对于遇到的每 1,然后增加计数并将其与迄今为止的最大值进行比较。如果遇到 0,则将计数重置为 0。
  4. 在第二次遍历中,按照上述过程找到所需的第二长连续 1 计数。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Function to find maximum
// and second maximum length
void FindMax(int arr[], int N)
{
    // Initialise maximum length
    int maxi = -1;
 
    // Initialise second maximum length
    int maxi2 = -1;
 
    // Initialise count
    int count = 0;
 
    // Iterate over the array
    for (int i = 0; i < N; ++i) {
 
        // If sequence ends
        if (arr[i] == 0)
 
            // Reset count
            count = 0;
 
        // Otherwise
        else {
 
            // Increase length
            // of current sequence
            count++;
 
            // Update maximum
            maxi = max(maxi, count);
        }
    }
 
    // Traverse the given array
    for (int i = 0; i < N; i++) {
 
        // If sequence continues
        if (arr[i] == 1) {
 
            // Increase length
            // of current sequence
            count++;
 
            // Update second max
            if (count > maxi2 && count < maxi) {
                maxi2 = count;
            }
        }
 
        // Reset count when 0 is found
        if (arr[i] == 0)
            count = 0;
    }
 
    maxi = max(maxi, 0);
    maxi2 = max(maxi2, 0);
 
    // Print the result
    cout << maxi2;
}
 
// Driver Code
int main()
{
    // Given array
    int arr[] = { 1, 1, 1, 0, 0, 1, 1 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    FindMax(arr, N);
    return 0;
}


Java
// Java implementation of the above approach
import java.io.*;
import java.util.*;
 
class GFG{
     
// Function to find maximum
// and second maximum length
static void FindMax(int arr[], int N)
{
     
    // Initialise maximum length
    int maxi = -1;
 
    // Initialise second maximum length
    int maxi2 = -1;
 
    // Initialise count
    int count = 0;
 
    // Iterate over the array
    for(int i = 0; i < N; ++i)
    {
         
        // If sequence ends
        if (arr[i] == 0)
 
            // Reset count
            count = 0;
 
        // Otherwise
        else
        {
             
            // Increase length
            // of current sequence
            count++;
 
            // Update maximum
            maxi = Math.max(maxi, count);
        }
    }
 
    // Traverse the given array
    for(int i = 0; i < N; i++)
    {
         
        // If sequence continues
        if (arr[i] == 1)
        {
             
            // Increase length
            // of current sequence
            count++;
 
            // Update second max
            if (count > maxi2 &&
                count < maxi)
            {
                maxi2 = count;
            }
        }
         
        // Reset count when 0 is found
        if (arr[i] == 0)
            count = 0;
    }
 
    maxi = Math.max(maxi, 0);
    maxi2 = Math.max(maxi2, 0);
 
    // Print the result
    System.out.println( maxi2);
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 1, 1, 1, 0, 0, 1, 1 };
    int n = arr.length;
     
    FindMax(arr, n);
}
}
 
// This code is contributed by Stream_Cipher


Python3
# Python3 implementation of the above approach
 
# Function to find maximum
# and second maximum length
def FindMax(arr, N):
   
    # Initialise maximum length
    maxi = -1
 
    # Initialise second maximum length
    maxi2 = -1
 
    # Initialise count
    count = 0
 
    # Iterate over the array
    for i in range(N):
 
        # If sequence ends
        if (arr[i] == 0):
 
            # Reset count
            count = 0
 
        # Otherwise
        else:
 
            # Increase length
            # of current sequence
            count += 1
 
            # Update maximum
            maxi = max(maxi, count)
 
    # Traverse the given array
    for i in range(N):
 
        # If sequence continues
        if (arr[i] == 1):
 
            # Increase length
            # of current sequence
            count += 1
 
            # Update second max
            if (count > maxi2 and
                count < maxi):
                maxi2 = count
 
        # Reset count when 0 is found
        if (arr[i] == 0):
            count = 0
 
    maxi = max(maxi, 0)
    maxi2 = max(maxi2, 0)
 
    # Print the result
    print(maxi2)
 
# Driver Code
if __name__ == '__main__':
   
    # Given array
    arr = [1, 1, 1, 0, 0, 1, 1]
    N = len(arr)
 
    # Function Call
    FindMax(arr, N)
 
# This code is contributed by Mohit Kumar29


C#
// C# implementation of the above approach
using System.Collections.Generic;
using System;
 
class GFG{
     
// Function to find maximum
// and second maximum length
static void FindMax(int []arr, int N)
{
     
    // Initialise maximum length
    int maxi = -1;
 
    // Initialise second maximum length
    int maxi2 = -1;
 
    // Initialise count
    int count = 0;
 
    // Iterate over the array
    for(int i = 0; i < N; ++i)
    {
         
        // If sequence ends
        if (arr[i] == 0)
 
            // Reset count
            count = 0;
 
        // Otherwise
        else
        {
             
            // Increase length
            // of current sequence
            count++;
 
            // Update maximum
            maxi = Math.Max(maxi, count);
        }
    }
     
    // Traverse the given array
    for(int i = 0; i < N; i++)
    {
 
        // If sequence continues
        if (arr[i] == 1)
        {
             
            // Increase length
            // of current sequence
            count++;
 
            // Update second max
            if (count > maxi2 &&
                count < maxi)
            {
                maxi2 = count;
            }
        }
         
        // Reset count when 0 is found
        if (arr[i] == 0)
            count = 0;
    }
     
    maxi = Math.Max(maxi, 0);
    maxi2 = Math.Max(maxi2, 0);
 
    // Print the result
    Console.WriteLine( maxi2);
}
 
// Driver code
public static void Main()
{
    int []arr = { 1, 1, 1, 0, 0, 1, 1 };
    int n = arr.Length;
     
    FindMax(arr, n);
}
}
 
// This code is contributed by Stream_Cipher


Javascript


输出:
2

时间复杂度: O(N)
辅助空间: O(1)

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