Related Articles

# Make array elements equal with minimum cost

• Last Updated : 02 Jun, 2021

Given an array of integers arr[], the task is to find the minimum number of steps to make array elements equal by following two operations –

• Add or subtract 2 from the element with 0 cost
• Add or subtract 1 from the element with 1 cost

Examples:

Input: arr[] = {4, 3, 2, 1}
Output:
Explanation:
As in the above example all the array elements can be equal to the 4 or 3 with 2 as minimum cost
Cost of Making Array Elements equal to 4
Index 0: As index 0 is already equal to 4, the cost is 0
Index 1: 4 – 3 = 1, Adding 1 to the element will cost 1
Index 2: 4 – 2 = 2, Adding 2 to the element will cost 0
Index 3: 4 – 1 = 3, Add 2 and 1 once in the array element, which will cost 1, because 2 can be added any number of times with cost 0.
Total Cost = 1 + 1 = 2

Input: arr[] = {3, 2, 1}
Output:
Explanation:
As in the above example all the array elements can be made equal to 3 or 1 with 1 as minimum cost
Cost of Making Array Elements equal to 3
Index 0: As index 0 is already equal to 3, the cost is 0
Index 1: 3 – 2 = 1, Adding 1 to the element will cost 1
Index 2: 3 – 1 = 2, Adding 2 to the element will cost 0
Total Cost = 1

Approach: The idea is to use the fact that adding 2 to any element of the array will cost 0. Also the second observation is that adding 2 to any odd number will result in an Odd number and similarly adding 2 to an even number will result in an even number only. Therefore, any element can be made equal to a nearest integer at zero cost but with the same property that it will remain odd if it is odd as initial value or it will remain even if it is even as initial. So, to find the minimum cost we can find the minimum count of the odd or even elements in the array.

Algorithm:

• Find the count of the odd or even elements in the array (say count).
• Find the minimum value between count and (length – count) where length is the length of the array.
• The minimum value will be desired cost to make all array elements equal.

Explanation with Example:

```Given Array be  - {4, 2, 3, 1}
Count of Odd Elements - 2 ({3, 1})
Count of Even Elements - 2 ({4, 2})

Hence, the minimum cost required is 2 and
the array elements can be made equal
to any integer of even or odd value```

Below is the implementation of the above approach:

## C++

 `// C++ implementation to make``// array elements equal with``// minimum cost` `#include ``using` `namespace` `std;` `// Function to find the minimum``// cost required to make array``// elements equal``void` `makearrayequal(``int` `arr[], ``int` `n)``{``    ``int` `x = 0;``    ` `    ``// Loop to find the``    ``// count of odd elements``    ``for` `(``int` `i = 0; i < n; i++) {``        ``x += arr[i] & 1;``    ``}` `    ``cout << min(x, n - x) << endl;``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 4, 3, 2, 1 };``    ``int` `n = ``sizeof``(arr) / ``sizeof``(arr);``    ``makearrayequal(arr, n);``    ``return` `0;``}`

## Java

 `// Java implementation to make``// array elements equal with``// minimum cost``class` `GFG {` `    ``// Function to find the minimum``    ``// cost required to make array``    ``// elements equal``    ``static` `void` `makearrayequal(``int` `arr[], ``int` `n)``    ``{``        ``int` `x = ``0``;``        ` `        ``// Loop to find the``        ``// count of odd elements``        ``for` `(``int` `i = ``0``; i < n; i++) {``            ``x += (arr[i] & ``1``);``        ``}``    ` `        ``System.out.println(Math.min(x, n - x));``    ``}``    ` `    ``// Driver Code``    ``public` `static` `void` `main (String[] args)``    ``{``        ``int` `arr[] = { ``4``, ``3``, ``2``, ``1` `};``        ``int` `n = arr.length;``        ``makearrayequal(arr, n);``       ` `    ``}``}` `// This code is contributed by Yash_R`

## Python3

 `# Python3 implementation to make``# array elements equal with``# minimum cost` `# Function to find the minimum``# cost required to make array``# elements equal``def` `makearrayequal(arr,  n) :``    ``x ``=` `0``;``    ` `    ``# Loop to find the``    ``# count of odd elements``    ``for` `i ``in` `range``(n) :``        ``x ``+``=` `arr[i] & ``1``;` `    ``print``(``min``(x, n ``-` `x));` `# Driver Code``if` `__name__ ``=``=` `"__main__"` `:` `    ``arr ``=` `[ ``4``, ``3``, ``2``, ``1` `];``    ``n ``=` `len``(arr);``    ``makearrayequal(arr, n);``  ` `# This code is contributed by Yash_R`

## C#

 `// C# implementation to make``// array elements equal with``// minimum cost``using` `System;` `class` `GFG {` `    ``// Function to find the minimum``    ``// cost required to make array``    ``// elements equal``    ``static` `void` `makearrayequal(``int` `[]arr, ``int` `n)``    ``{``        ``int` `x = 0;``        ` `        ``// Loop to find the``        ``// count of odd elements``        ``for` `(``int` `i = 0; i < n; i++) {``            ``x += (arr[i] & 1);``        ``}``    ` `        ``Console.WriteLine(Math.Min(x, n - x));``    ``}``    ` `    ``// Driver Code``    ``public` `static` `void` `Main (``string``[] args)``    ``{``        ``int` `[]arr = { 4, 3, 2, 1 };``        ``int` `n = arr.Length;``        ``makearrayequal(arr, n);``       ` `    ``}``}` `// This code is contributed by Yash_R`

## Javascript

 ``
Output:
`2`

Performance Analysis:

• Time Complexity: O(N).
• Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up