Count subarrays having an equal count of 0 s and 1 s segregated | ''Function to count subarrays having equal count of 0s and 1s with all 0s and all 1s grouped together ; '' Stores the count ; '' Initialize cur with first element ; '' If the next element is same  as the current element ; '' Increment count ; '' Update curr ; '' Iterate over the array count ; '' Consider the minimum ;  ; ''Given arr[] ; ''Function Call
def countSubarrays ( A ) : NEW_LINE INDENT res = 0 NEW_LINE curr , cnt = A [ 0 ] , [ 1 ] NEW_LINE for c in A [ 1 : ] : NEW_LINE INDENT if c == curr : NEW_LINE INDENT cnt [ - 1 ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT curr = c NEW_LINE DEDENT cnt . append ( 1 ) NEW_LINE DEDENT for i in range ( 1 , len ( cnt ) ) : NEW_LINE INDENT res += min ( cnt [ i - 1 ] , cnt [ i ] ) NEW_LINE DEDENT print ( res - 1 ) NEW_LINE DEDENT / * Driver code * / NEW_LINE A = [ 1 , 1 , 0 , 0 , 1 , 0 ] NEW_LINE countSubarrays ( A ) NEW_LINE