Minimum subarray reversals required to make given binary array alternating | Function to count minimum subarray reversal operations required to make array alternating ; Stores minimum count of operations required to make array alternating ; Traverse the array ; If arr [ i ] is greater than arr [ i + 1 ] ; Update cntOp ; Driver code
function minimumcntOperationReq ( arr , N ) { let cntOp = 0 ; for ( let i = 0 ; i < N - 1 ; i ++ ) { if ( arr [ i ] == arr [ i + 1 ] ) { cntOp ++ ; } } return ( cntOp + 1 ) / 2 ; } let arr = [ 1 , 1 , 1 , 0 , 1 , 0 , 0 , 0 ] ; let N = arr . length ; document . write ( Math . floor ( minimumcntOperationReq ( arr , N ) ) ) ;