Related Articles

Maximum consecutive one’s (or zeros) in a binary circular array

• Difficulty Level : Hard
• Last Updated : 18 May, 2021

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:
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:

1. 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.
2. 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.
3. 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++

 `// 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

 ``
Output
`6`

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up