Minimum subarray reversals required such that sum of all pairs of adjacent elements is odd | Function to count reversals to separate elements with same parity ; Traverse the given array ; Count size of subarray having letegers with same parity only ; Otherwise ; Reversals required is equal to one less than subarray size ; Return the total reversals ; Function to prlet the array elements ; Function to count the minimum reversals required to make make sum of all adjacent elements odd ; Stores operations required for separating adjacent odd elements ; Stores operations required for separating adjacent even elements ; Maximum of two is the return ; Given array arr [ ] ; Size of array ; Function Call
function separate ( arr , n , parity ) { let count = 1 , res = 0 ; for ( let i = 1 ; i < n ; i ++ ) { if ( ( ( arr [ i ] + parity ) & 1 ) != 0 && ( ( arr [ i - 1 ] + parity ) & 1 ) != 0 ) count ++ ; else { if ( count > 1 ) res += count - 1 ; count = 1 ; } } return res ; } function prletArray ( arr , n ) { for ( let i = 0 ; i < n ; i ++ ) document . write ( arr [ i ] + " " ) ; } function requiredOps ( arr , N ) { let res1 = separate ( arr , N , 0 ) ; let res2 = separate ( arr , N , 1 ) ; document . write ( Math . max ( res1 , res2 ) ) ; } let arr = [ 13 , 2 , 6 , 8 , 3 , 5 , 7 , 10 , 14 , 15 ] ; let N = arr . length ; requiredOps ( arr , N ) ;