Menu Close

Find the count of consecutive 1’s present in array.

Given binary array, find count of maximum number of consecutive 1’s present in the array.

Input  : arr[] = {1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1}
Output : 4

Input  : arr[] = {0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1}
Output : 1
def maxibin(arr):
    temp=0
    count=0
    for i in range(len(arr)):
        if arr[i]==0 :
            count=0
        else:
            count+=1
            temp=max(count,temp)
    return temp

Arry=list(map(int,input('arr[] : ').split(' ')))
print(maxibin(Arry))

Input_1:
arr[ ] : 1 1 0 0 1 0 1 0 1 1 1 1

Output:
4


Input_2:
arr[ ] : 0 0 1 0 1 0 1 0 1 0 1
Output
1


Input_3:
arr[ ] : 1 1 1 0 1 0 1 0 1 1 1 0
Output:
3


Input_4:
arr[ ] : 1 1 1 0 1 1 0 1 1 1 1 0 1 1 1
Output:
4


Illustration of the Output:

Executed using python3

More Q