Minimum cost to make all array elements equal | Java implementation of the approach ; 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
class GFG { static int minCost ( int arr [ ] , int n ) { int count_even = 0 ; int count_odd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) count_even ++ ; else count_odd ++ ; } return Math . min ( count_even , count_odd ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 3 , 1 , 5 } ; int n = arr . length ; System . out . println ( minCost ( arr , n ) ) ; } }