Maximum consecutive one’s (or zeros) in a binary circular array
Last Updated :
21 Jun, 2022
Given a binary circular array of size N, the task is to find the count maximum number of consecutive 1’s present in the circular array.
Examples:
Input: arr[] = {1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1}
Output: 6
The last 4 and first 2 positions have 6 consecutive ones.
Input: a[] = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1}
Output: 1
A Naive Solution is create an array of size 2*N where N is size of input array. We copy the input array twice in this array. Now we need to find the longest consecutive 1s in this binary array in one traversal.
Efficient Solution : The following steps can be followed to solve the above problem:
- Instead of creating an array of size 2*N to implement the circular array, we can use the modulus operator to traverse the array circularly.
- Iterate from 0 to 2*N and find the consecutive number of 1’s as:
- Traverse array from left to right.
- Everytime while traversing, calculate the current index as (i%N) in order to traverse the array circularly when i>N.
- If we see a 1, we increment count and compare it with maximum so far. If we see a 0, we reset count as 0.
- Break out of the loop when a[i]==0 and i>=n to reduce the time complexity in some cases.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getMaxLength( bool arr[], int n)
{
int start = 0;
int preCnt = 0;
while (start < n && arr[start] == 1) {
preCnt++;
start++;
}
int end = n - 1;
int suffCnt = 0;
while (end >= 0 && arr[end] == 1) {
suffCnt++;
end--;
}
if (start > end)
return n;
int midCnt = 0;
int result = 0;
for ( int i = start; i <= end; i++) {
if (arr[i] == 1) {
midCnt++;
result = max(result, midCnt);
}
else {
midCnt = 0;
}
}
return max(result, preCnt + suffCnt);
}
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
class GfG {
static int getMaxLength( int arr[], int n)
{
int start = 0 ;
int preCnt = 0 ;
while (start < n && arr[start] == 1 ) {
preCnt++;
start++;
}
int end = n - 1 ;
int suffCnt = 0 ;
while (end >= 0 && arr[end] == 1 ) {
suffCnt++;
end--;
}
if (start > end)
return n;
int midCnt = 0 ;
int result = 0 ;
for ( int i = start; i <= end; i++) {
if (arr[i] == 1 ) {
midCnt++;
result = Math.max(result, midCnt);
}
else {
midCnt = 0 ;
}
}
return Math.max(result, preCnt + suffCnt);
}
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));
}
}
|
Python3
def getMaxLength(arr, n):
start = 0
preCnt = 0
while (start < n and arr[start] = = 1 ):
preCnt = preCnt + 1
start = start + 1
end = n - 1
suffCnt = 0
while (end > = 0 and arr[end] = = 1 ):
suffCnt = suffCnt + 1
end = end - 1
if (start > end):
return n
midCnt = 0
i = start
result = 0
while (i < = end):
if (arr[i] = = 1 ):
midCnt = midCnt + 1
result = max (result, midCnt)
else :
midCnt = 0
i = i + 1
return max (result, preCnt + suffCnt)
if __name__ = = '__main__' :
arr = [ 1 , 1 , 0 , 0 , 1 , 0 ,
1 , 0 , 1 , 1 , 1 , 1 ]
n = len (arr)
print (getMaxLength(arr, n))
|
C#
using System;
class GFG {
static int getMaxLength( int [] arr, int n)
{
int start = 0;
int preCnt = 0;
while (start < n && arr[start] == 1) {
preCnt++;
start++;
}
int end = n - 1;
int suffCnt = 0;
while (end >= 0 && arr[end] == 1) {
suffCnt++;
end--;
}
if (start > end)
return n;
int midCnt = 0;
int result = 0;
for ( int i = start; i <= end; i++) {
if (arr[i] == 1) {
midCnt++;
result = Math.Max(result, midCnt);
}
else {
midCnt = 0;
}
}
return Math.Max(result, preCnt + suffCnt);
}
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));
}
}
|
Javascript
<script>
function getMaxLength(arr , n)
{
var start = 0;
var preCnt = 0;
while (start < n && arr[start] == 1) {
preCnt++;
start++;
}
var end = n - 1;
var suffCnt = 0;
while (end >= 0 && arr[end] == 1) {
suffCnt++;
end--;
}
if (start > end)
return n;
var midCnt = 0;
var result = 0;
for (i = start; i <= end; i++) {
if (arr[i] == 1) {
midCnt++;
result = Math.max(result, midCnt);
} else {
midCnt = 0;
}
}
return Math.max(result, preCnt + suffCnt);
}
var arr = [ 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1 ];
var n = arr.length;
document.write(getMaxLength(arr, n));
</script>
|
Time Complexity: O(N), as we are using a loop to traverse N times. Where N is the number of elements in the array.
Auxiliary Space: O(1), as we are not using any extra space.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...