# Minimum cost to make all array elements equal

Given an array **arr[]** consisting of **N** positive integers, the task is to make all values of this array equal to some integer value with minimum cost after performing below operations any number of times (possibly zero).

- Reduce the array element by 2 or increase it by 2 with zero cost.
- Reduce the array element by 1 or increase it by 1 with a unit cost.

**Examples:**

Input:arr[] = {2, 4, 3, 1, 5}

Output:2

We can change 3rd element to 5 incurring 0 cost.

We can change the 4th element to 5 ( 1 -> 3 -> 5 ) incurring 0 cost.

Now the array is, 2 4 5 5 5

We can change the 1st element to 5 (2 -> 4 -> 5 ) incurring unit cost.

We can change the 2nd element to 5 incurring unit cost.

Final array is, 5 5 5 5 5

Total cost = 1 + 1 = 2

Input:arr[] = {2, 2, 2, 3}

Output:1

We can decrement last element by 1 incurring unit cost only.

**Approach:** The basic idea is to count the number of even elements and odd elements present in the array and print the minimum of these two as the answer. This approach works because we can make all even elements equal and all odd elements equal without incurring any cost (Operation 1). The updated array after performing these operations will only contain elements x and x + 1 where one is odd and the other is even. The elements from both the types can be changed into the other type with 1 unit cost and in order to minimise the cost, the result will be the min(frequency(x), frequency(x + 1)).

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the minimum cost ` `// to make each array element equal ` `int` `minCost(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// To store the count of even numbers ` ` ` `// present in the array ` ` ` `int` `count_even = 0; ` ` ` ` ` `// To store the count of odd numbers ` ` ` `// present in the array ` ` ` `int` `count_odd = 0; ` ` ` ` ` `// Iterate through the array and ` ` ` `// find the count of even numbers ` ` ` `// and odd numbers ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(arr[i] % 2 == 0) ` ` ` `count_even++; ` ` ` `else` ` ` `count_odd++; ` ` ` `} ` ` ` ` ` `return` `min(count_even, count_odd); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 2, 4, 3, 1, 5 }; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` ` ` `cout << minCost(arr, n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `// Function to return the minimum cost ` `// to make each array element equal ` `static` `int` `minCost(` `int` `arr[], ` `int` `n) ` `{ ` ` ` `// To store the count of even numbers ` ` ` `// present in the array ` ` ` `int` `count_even = ` `0` `; ` ` ` ` ` `// To store the count of odd numbers ` ` ` `// present in the array ` ` ` `int` `count_odd = ` `0` `; ` ` ` ` ` `// Iterate through the array and ` ` ` `// find the count of even numbers ` ` ` `// and odd numbers ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `if` `(arr[i] % ` `2` `== ` `0` `) ` ` ` `count_even++; ` ` ` `else` ` ` `count_odd++; ` ` ` `} ` ` ` ` ` `return` `Math.min(count_even, count_odd); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `arr[] = { ` `2` `, ` `4` `, ` `3` `, ` `1` `, ` `5` `}; ` ` ` `int` `n = arr.length; ` ` ` ` ` `System.out.println(minCost(arr, n)); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` ` ` `# Function to return the minimum cost ` `# to make each array element equal ` `def` `minCost(arr, n): ` ` ` ` ` `# To store the count of even numbers ` ` ` `# present in the array ` ` ` `count_even ` `=` `0` ` ` ` ` `# To store the count of odd numbers ` ` ` `# present in the array ` ` ` `count_odd ` `=` `0` ` ` ` ` `# Iterate through the array and ` ` ` `# find the count of even numbers ` ` ` `# and odd numbers ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `if` `(arr[i] ` `%` `2` `=` `=` `0` `): ` ` ` `count_even ` `+` `=` `1` ` ` `else` `: ` ` ` `count_odd ` `+` `=` `1` ` ` ` ` `return` `min` `(count_even, count_odd) ` ` ` `# Driver code ` `arr ` `=` `[` `2` `, ` `4` `, ` `3` `, ` `1` `, ` `5` `] ` `n ` `=` `len` `(arr) ` ` ` `print` `(minCost(arr, n)) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return the minimum cost ` `// to make each array element equal ` `static` `int` `minCost(` `int` `[]arr, ` `int` `n) ` `{ ` ` ` `// To store the count of even numbers ` ` ` `// present in the array ` ` ` `int` `count_even = 0; ` ` ` ` ` `// To store the count of odd numbers ` ` ` `// present in the array ` ` ` `int` `count_odd = 0; ` ` ` ` ` `// Iterate through the array and ` ` ` `// find the count of even numbers ` ` ` `// and odd numbers ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `if` `(arr[i] % 2 == 0) ` ` ` `count_even++; ` ` ` `else` ` ` `count_odd++; ` ` ` `} ` ` ` ` ` `return` `Math.Min(count_even, count_odd); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `[]arr = { 2, 4, 3, 1, 5 }; ` ` ` `int` `n = arr.Length; ` ` ` ` ` `Console.WriteLine(minCost(arr, n)); ` `} ` `} ` ` ` `// This code is contributed by Princi Singh ` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity:** O(N)

**Space Complexity:** O(1)

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

## Recommended Posts:

- Make all array elements equal with minimum cost
- Make array elements equal with minimum cost
- Minimum Cost to make all array elements equal using given operations
- Minimum cost to equal all elements of array using two operation
- Minimum operations to make all elements equal using the second array
- Minimum operation to make all elements equal in array
- Minimum number of steps to make all elements of array equal
- Minimum Increment / decrement to make array elements equal
- Minimum Bitwise AND operations to make any two array elements equal
- Minimum value of X to make all array elements equal by either decreasing or increasing by X
- Minimum operations required to make all the array elements equal
- Minimum Bitwise XOR operations to make any two array elements equal
- Minimum Bitwise OR operations to make any two array elements equal
- Minimum number of increment-other operations to make all array elements equal.
- Minimum decrement operations to make Array elements equal by only decreasing K each time

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.