给定大小为N的二进制数组arr [] ,任务是找到数组中存在的连续的1的第二个最长序列的长度。
例子:
Input: arr[] = {1, 1, 1, 0, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0}
Output: 4 3
Explanation:
Longest sequence of consecutive ones is 4 i.e {arr[7], … arr[10]}.
Second longest sequence of consecutive ones is 3 i.e {arr[0], … arr[2]}.
Input: arr[] = {1, 0, 1}
Output: 1 0
方法:想法是遍历给定的二进制数组,并跟踪到目前为止遇到的连续数组的最大和第二最大长度。步骤如下:
- 初始化变量maxi , count和second_max分别存储连续的1的最长,当前和第二最长序列的长度。
- 在给定的数组上迭代两次。
- 首先,从左到右遍历数组。对于遇到的每1个,然后递增计数并将其与到目前为止的最大值进行比较。如果遇到0,则将计数重置为0。
- 在第二遍历中,按照上述步骤找到所需的连续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)