Minimum flips of odd indexed elements from odd length subarrays to make two given arrays equal | Function to find the minimum flip of subarrays required at alternate index to make binary arrays equals ; Stores count of total operations ; Stores count of consecutive unequal elements ; Loop to run on odd positions ; Incrementing the global counter ; Change count to 0 ; If all last elements are equal ; Loop to run on even positions ; Incrementing the global counter ; Change count to 0 ; Print the minimum operations ; Driver code ; Function Call
function minOperation ( X , Y , n ) { let C = 0 ; let count = 0 ; for ( let i = 1 ; i < n ; i = i + 2 ) { if ( X [ i ] != Y [ i ] ) { count ++ ; } else { if ( count != 0 ) C ++ ; count = 0 ; } } if ( count != 0 ) C ++ ; count = 0 ; for ( let i = 0 ; i < n ; i = i + 2 ) { if ( X [ i ] != Y [ i ] ) { count ++ ; } else { if ( count != 0 ) C ++ ; count = 0 ; } } if ( count != 0 ) C ++ ; document . write ( C ) ; } let X = [ 1 , 0 , 0 , 0 , 0 , 1 ] ; let Y = [ 1 , 1 , 0 , 1 , 1 , 1 ] ; let N = X . length ; minOperation ( X , Y , N ) ;