Given a binary array arr[] of size N, the task is to find the length of the second longest sequence of consecutive 1s present in the array.
Examples:
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
Approach: The idea is to traverse the given binary array and keep track of the largest and the second largest length of consecutive one’s encountered so far. Below are the steps:
- Initialise variables maxi, count and second_max to store the length of the longest, current and second longest sequence of consecutive 1s respectively..
- Iterate over the given array twice.
- First, traverse the array from left to right. For every 1 encountered, then increment count and compare it with maximum so far. If 0 is encountered, reset count as 0.
- In the second traversal, find the required second longest count of consecutive 1’s following the above procedure.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void FindMax( int arr[], int N)
{
int maxi = -1;
int maxi2 = -1;
int count = 0;
for ( int i = 0; i < N; ++i) {
if (arr[i] == 0)
count = 0;
else {
count++;
maxi = max(maxi, count);
}
}
for ( int i = 0; i < N; i++) {
if (arr[i] == 1) {
count++;
if (count > maxi2 && count < maxi) {
maxi2 = count;
}
}
if (arr[i] == 0)
count = 0;
}
maxi = max(maxi, 0);
maxi2 = max(maxi2, 0);
cout << maxi2;
}
int main()
{
int arr[] = { 1, 1, 1, 0, 0, 1, 1 };
int N = sizeof (arr) / sizeof (arr[0]);
FindMax(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG{
static void FindMax( int arr[], int N)
{
int maxi = - 1 ;
int maxi2 = - 1 ;
int count = 0 ;
for ( int i = 0 ; i < N; ++i)
{
if (arr[i] == 0 )
count = 0 ;
else
{
count++;
maxi = Math.max(maxi, count);
}
}
for ( int i = 0 ; i < N; i++)
{
if (arr[i] == 1 )
{
count++;
if (count > maxi2 &&
count < maxi)
{
maxi2 = count;
}
}
if (arr[i] == 0 )
count = 0 ;
}
maxi = Math.max(maxi, 0 );
maxi2 = Math.max(maxi2, 0 );
System.out.println( maxi2);
}
public static void main(String[] args)
{
int arr[] = { 1 , 1 , 1 , 0 , 0 , 1 , 1 };
int n = arr.length;
FindMax(arr, n);
}
}
|
Python3
def FindMax(arr, N):
maxi = - 1
maxi2 = - 1
count = 0
for i in range (N):
if (arr[i] = = 0 ):
count = 0
else :
count + = 1
maxi = max (maxi, count)
for i in range (N):
if (arr[i] = = 1 ):
count + = 1
if (count > maxi2 and
count < maxi):
maxi2 = count
if (arr[i] = = 0 ):
count = 0
maxi = max (maxi, 0 )
maxi2 = max (maxi2, 0 )
print (maxi2)
if __name__ = = '__main__' :
arr = [ 1 , 1 , 1 , 0 , 0 , 1 , 1 ]
N = len (arr)
FindMax(arr, N)
|
C#
using System.Collections.Generic;
using System;
class GFG{
static void FindMax( int []arr, int N)
{
int maxi = -1;
int maxi2 = -1;
int count = 0;
for ( int i = 0; i < N; ++i)
{
if (arr[i] == 0)
count = 0;
else
{
count++;
maxi = Math.Max(maxi, count);
}
}
for ( int i = 0; i < N; i++)
{
if (arr[i] == 1)
{
count++;
if (count > maxi2 &&
count < maxi)
{
maxi2 = count;
}
}
if (arr[i] == 0)
count = 0;
}
maxi = Math.Max(maxi, 0);
maxi2 = Math.Max(maxi2, 0);
Console.WriteLine( maxi2);
}
public static void Main()
{
int []arr = { 1, 1, 1, 0, 0, 1, 1 };
int n = arr.Length;
FindMax(arr, n);
}
}
|
Javascript
<script>
function FindMax(arr, N)
{
let maxi = -1;
let maxi2 = -1;
let count = 0;
for (let i = 0; i < N; ++i)
{
if (arr[i] == 0)
count = 0;
else
{
count++;
maxi = Math.max(maxi, count);
}
}
for (let i = 0; i < N; i++)
{
if (arr[i] == 1)
{
count++;
if (count > maxi2 &&
count < maxi)
{
maxi2 = count;
}
}
if (arr[i] == 0)
count = 0;
}
maxi = Math.max(maxi, 0);
maxi2 = Math.max(maxi2, 0);
document.write( maxi2);
}
let arr = [ 1, 1, 1, 0, 0, 1, 1 ];
let n = arr.length;
FindMax(arr, n);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)