Minimum cost to make all array elements equal | Function to return the minimum cost to make each array element equal ; To store the count of even numbers present in the array ; To store the count of odd numbers present in the array ; Iterate through the array and find the count of even numbers and odd numbers ; Driver code
function minCost ( arr , n ) { var count_even = 0 ; var count_odd = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) count_even ++ ; else count_odd ++ ; } return Math . min ( count_even , count_odd ) ; } var arr = [ 2 , 4 , 3 , 1 , 5 ] ; var n = arr . length ; document . write ( minCost ( arr , n ) ) ;