- Related Questions & Answers
- Maximum length of consecutive 1’s in a binary string in Python using Map function
- Maximum 0’s between two immediate 1’s in binary representation in C++
- Count 1’s in a sorted binary array in C++
- Program to Count number of binary strings without consecutive 1’s in C/C++?
- C/C++ Program to Count number of binary strings without consecutive 1’s?
- C# program to check if there are K consecutive 1’s in a binary number
- Count the number of 1’s and 0’s in a binary array using STL in C++
- Maximum Consecutive Zeroes in Concatenated Binary String in C++
- One’s Complement
- Maximum Consecutive Increasing Path Length in Binary Tree in C++
- Maximum difference of zeros and ones in binary string in C++
- Maximum sub-matrix area having count of 1’s one more than count of 0’s in C++
- Python program to check if there are K consecutive 1’s in a binary number?
- 1’s and 2’s complement of a Binary Number?
- Maximum length of segments of 0’s and 1’s in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are given with a circular array. Circular array is the array for which we consider the case that the first element comes next to the last element. It is used to implement queues. So we have to count the maximum no. of consecutive 1’s or 0’s in that array.

Let’s understand with examples.

**Input** − Arr[] = { 1,1,0,1,0,1,0,1,1,1 }

**Output** − Maximum consecutive 1’s are 5. Or Maximum consecutive 0’s is 1.

**Explanation** − From Arr[] index 7 to 9 and then indexes 0 and 1. 1’s are 5. No consecutive 0’s but 1.

**Input** − Arr[] = { 0,0,0,1,0 }

**Output** − Maximum consecutive 1’s is1. Or Maximum consecutive 0’s are 4.

**Explanation** − From Arr[] index 4 and then indexes 0 to 3. 0’s are 4.

We take an input Arr[] which contains 0’s and 1’s in random order.

Variable N is used for the size of the Arr[].

Bit is used to store the 1 or 0, according to which we will count.

Function maxConsecutive(int arr[], int n, int bit) takes three input parameters. The array itself, its size and 0 or 1 as bit. Returns the count of bits past.

To make the array circular. Temp[2*n] is used to store the arr[] twice in it. While() loop runs twice to copy arr[] into temp.

Now we will count consecutive 1’s ( or 0’s ) using while ( temp[k++]==bit ) and store consecutive count in variable ‘count’.

If this count is maximum found so far, store it in maxC.

Return the maxC as a final result.

#include <iostream> //to return maximum bishops possible int maxConsecutive(int arr[],int n,int bit){ int count=0; int temp[2*n]={0}; int maxC=0; int j=0,k=0; //to twice copy arr[] while(j<2){ for(int i=0;i<n;i++){ temp[k++]=arr[i]; } j++; } k=0; for(int i=0;i<2*n; i++){ count=0; while(temp[k++]==bit){ ++count; } if(maxC<count) maxC=count; } return maxC; } int main(){ int Arr[]={1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1 }; int N = 12; int bit=1; printf("Maximum Consecutive 1's in circular array: %d",maxConsecutive(Arr,N,bit)); bit=0; printf("\nMaximum Consecutive 0's in circular array: %d",maxConsecutive(Arr,N,bit)); return 0; }

If we run the above code it will generate the following output −

Maximum Consecutive 1's in circular array: 6 Maximum Consecutive 0's in circular array: 2

Advertisements