text: Count subarrays having an equal count of 0 s and 1 s segregated | Function to count subarrays having equal count of 0 s and 1 s with all 0 s and all 1 s 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
code: function countSubarrays ( A , N ) { var res = 0 ; var curr = A [ 0 ] ; var cnt = [ ] ; cnt . fill ( 1 ) for ( var c = 1 ; c < N ; c ++ ) { if ( A == curr ) cnt [ cnt . length - 1 ] ++ ; else curr = A ; cnt . push ( 1 ) ; } for ( var i = 1 ; i < cnt . length ; i ++ ) { res += Math . min ( cnt [ i - 1 ] , cnt [ i ] ) ; } document . write ( res ) ; } var A = [ 1 , 1 , 0 , 0 , 1 , 0 ] ; var N = A . length ; countSubarrays ( A , N ) ;