Minimum cost required to move all elements to the same position | C ++ 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
#include <iostream> NEW_LINE using namespace std ; int minCost ( int arr [ ] , int arr_size ) { int odd = 0 , even = 0 ; for ( int i = 0 ; i < arr_size ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even ++ ; else odd ++ ; } cout << min ( even , odd ) ; } int main ( ) { int arr [ ] = { 1 , 2 , 3 } ; int arr_size = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; minCost ( arr , arr_size ) ; }