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++
// C++ program to count maximum consecutive
// 1's in a binary circular array
#include <bits/stdc++.h>
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
<script>
// javascript 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
function getMaxLength(arr , n)
{
// Starting index
var start = 0;
// To store the maximum length of the
// prefix of the given array with all 1s
var preCnt = 0;
while (start < n && arr[start] == 1) {
preCnt++;
start++;
}
// Ending index
var end = n - 1;
// To store the maximum length of the
// suffix of the given array with all 1s
var 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
var midCnt = 0;
// To store the result for middle 1s
var result = 0;
for (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
var arr = [ 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1 ];
var n = arr.length;
document.write(getMaxLength(arr, n));
// This code is contributed by umadevi9616
</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.
Similar Reads
Maximum consecutive oneâs (or zeros) in a binary array Given an array arr[] consisting of only 0's and 1's, the task is to find the count of a maximum number of consecutive 1's or 0's present in the array.Examples : Input: arr[] = {1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1}Output: 4Explanation: The maximum number of consecutive 1's in the array is 4 from index
7 min read
Maximum Consecutive Zeroes in Concatenated Binary String You are given a binary string str of length n. Suppose you create another string of size n * k by concatenating k copies of str together. What is the maximum size of a substring of the concatenated string consisting only of 0's? Given that k > 1. Examples: Input : str = "110010", k = 2 Output : 2
8 min read
Maximize sum of consecutive differences in a circular array Given an array of n elements. Consider array as circular array i.e element after an is a1. The task is to find maximum sum of the difference between consecutive elements with rearrangement of array element allowed i.e after rearrangement of element find |a1 - a2| + |a2 - a3| + ...... + |an - 1 - an|
5 min read
Maximum Consecutive Ones After Flipping Zeroes Given a binary array arr[] and an integer k, find the maximum length of a subarray containing all ones after flipping at most k zeroes to 1's.Examples: Input: arr[] = {1, 0, 1}, k = 1Output: 3Explanation: By flipping the zero at index 1, all the array elements become one.Input: arr[] = {1, 0, 0, 1,
10 min read
Maximum number of consecutive 1's in binary representation of all the array elements Given an array arr[] of N elements, the task is to find the maximum number of consecutive 1's in the binary representation of an element among all the elements of the given array. Examples: Input: arr[] = {1, 2, 3, 4} Output: 2 Binary(1) = 01 Binary(2) = 10 Binary(3) = 11 Binary(4) = 100 Input: arr[
6 min read
Minimum swaps to group all 0s together in Binary Circular Array Given a binary circular array arr[] of size N, the task is to find the minimum swaps to group all 0s together in the array. Examples: Input: arr[] = {1, 0, 1, 0, 0, 1, 1}Output: 1Explanation: Here are a few of the ways to group all the 0's together: {1, 1, 0, 0, 0, 1, 1} using 1 swap.{1, 0, 0, 0, 1,
6 min read
Maximum difference of zeros and ones in binary string Given a binary string of 0s and 1s. The task is to find the length of the substring which is having a maximum difference between the number of 0s and the number of 1s (number of 0s - number of 1s). In case of all 1s print -1. Examples: Input : S = "11000010001"Output : 6From index 2 to index 9, ther
15 min read
Length of second longest sequence of consecutive 1s in a binary array 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]}
7 min read
Minimum bit changes in Binary Circular array to reach a index Given a Binary Circular Array of size N elements and two positive integers x and y indicating the indices in the circular array. The task is check which path, clockwise or anti-clockwise, from index x to index y, we face the minimum number bit flips. Output "Clockwise" or "Anti-clockwise" and the va
9 min read
Maximum difference of zeros and ones in binary string | Set 2 (O(n) time) Given a binary string of 0s and 1s. The task is to find the maximum difference between the number of 0s and number of 1s in any sub-string of the given binary string. That is maximize ( number of 0s - number of 1s ) for any sub-string in the given binary string. Examples: Input : S = "11000010001" O
7 min read