Maximum consecutive one’s (or zeros) in a binary circular array
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
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) { // intitialize count int count = 0; // initialize max int result = 0; for (int i = 0; i < 2 * n; i++) { // Reset count when 0 is found // use mod operator for circular array if (arr[i % n] == 0) { count = 0; // Condition to reduce time complexity if (i >= n) break; } // If 1 is found, increment count // and update result if count becomes // more. else { // increase count count++; result = max(result, count); } } return result; } // 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) << endl; 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) { // intitialize count int count = 0; // initialize max int result = 0; for (int i = 0; i < 2 * n; i++) { // Reset count when 0 is found // use mod operator for circular array if(arr[i % n] == 0) { count = 0; // Condition to reduce time complexity if (i >= n) break; } // If 1 is found, increment count // and update result if count becomes // more. else { // increase count count++; result = Math.max(result, count); } } return result; } // 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): # intitialize count count = 0 # initialize max result = 0 for i in range(2 * n): # Reset count when 0 is found # use mod operator for circular array if (arr[i % n] == 0): count = 0 # Condition to reduce time complexity if (i >= n): break # If 1 is found, increment count # and update result if count # becomes more. else: # increase count count += 1 result = max(result, count) return result # 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) { // intitialize count int count = 0; // initialize max int result = 0; for (int i = 0; i < 2 * n; i++) { // Reset count when 0 is found // use mod operator for circular array if(arr[i % n] == 0) { count = 0; // Condition to reduce time complexity if (i >= n) break; } // If 1 is found, increment count // and update result if count becomes // more. else { // increase count count++; result = Math.Max(result, count); } } return result; } // Driver code public static void Main() { int[] arr = new int[] { 1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1 }; int n = arr.Length; Console.WriteLine(getMaxLength(arr, n)); } } // This code is contributed by Code_Mech. |
PHP
= $n)
break;
}
// If 1 is found, increment count
// and update result if count becomes
// more.
else
{
// increase count
$count++;
$result = max($result, $count);
}
}
return $result;
}
// Driver code
$arr = array(1, 1, 0, 0, 1, 0,
1, 0, 1, 1, 1, 1 );
$n = sizeof($arr);
echo(getMaxLength($arr, $n));
// This code is contributed by Code_Mech
?>
6
Recommended Posts:
- Maximum consecutive one’s (or zeros) in a binary array
- Maximum difference of zeros and ones in binary string
- Maximum difference of zeros and ones in binary string | Set 2 (O(n) time)
- Maximize sum of consecutive differences in a circular array
- Maximum Consecutive Zeroes in Concatenated Binary String
- Maximum sum in circular array such that no two elements are adjacent
- Minimum bit changes in Binary Circular array to reach a index
- Maximum consecutive numbers present in an array
- Count binary strings with twice zeros in first half
- Maximum circular subarray sum
- Remove leading zeros from an array
- Move all zeros to start and ones to end in an Array of random integers
- Find d to maximize the number of zeros in array c[] created as c[i] = d*a[i] + b[i]
- Count number of trailing zeros in product of array
- Maximum subsequence sum such that no three are consecutive
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



