Minimize increments required to make count of even and odd array elements equal | Function to find min operations to make even and odd count equal ; Odd size will never make odd and even counts equal ; Stores the count of even numbers in the array arr ; Stores count of odd numbers in the array arr ; Traverse the array arr ; If arr [ i ] is an even number ; Update cntEven ; Odd numbers in arr ; Return absolute difference divided by 2 ; Driver code ; Function call
function minimumIncrement ( arr , N ) { if ( N % 2 != 0 ) { document . write ( " " ) ; System . exit ( 0 ) ; } var cntEven = 0 ; var cntOdd = 0 ; for ( i = 0 ; i < N ; i ++ ) { if ( arr [ i ] % 2 == 0 ) { cntEven += 1 ; } } cntOdd = N - cntEven ; return Math . abs ( cntEven - cntOdd ) / 2 ; } var arr = [ 1 , 3 , 4 , 9 ] ; var N = arr . length ; document . write ( minimumIncrement ( arr , N ) ) ;