Minimum cost required to move all elements to the same position | Java program to implement the above approach ; Function to find the minimum cost required to place all elements in the same position ; Stores the count of even and odd elements ; Traverse the array arr [ ] ; Count even elements ; Count odd elements ; Print the minimum count ; Driver Code ; Given array ; Function Call
import java . io . * ; class GFG { public void minCost ( int [ ] arr ) { int odd = 0 , even = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even ++ ; else odd ++ ; } System . out . print ( Math . min ( even , odd ) ) ; } public static void main ( String [ ] args ) { GFG obj = new GFG ( ) ; int arr [ ] = { 1 , 2 , 3 } ; obj . minCost ( arr ) ; } }