Open In App

Make array elements equal with minimum cost

Improve
Improve
Like Article
Like
Save
Share
Report

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 <bits/stdc++.h>
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[0]);
    makearrayequal(arr, n);
    return 0;
}


Java




// Java implementation to make
// array elements equal with
// minimum cost
import java.io.*;
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




<script>
// javascript implementation to make
// array elements equal with
// minimum cost
 
// Function to find the minimum
// cost required to make array
// elements equal
function makearrayequal(arr , n)
{
    var x = 0;
     
    // Loop to find the
    // count of odd elements
    for (i = 0; i < n; i++)
    {
        x += (arr[i] & 1);
    }
    document.write(Math.min(x, n - x));
}
 
// Driver Code
var arr = [ 4, 3, 2, 1 ];
var n = arr.length;
makearrayequal(arr, n);
 
// This code is contributed by Amit Katiyar
</script>


Output: 

2

 

Performance Analysis: 

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

 



Last Updated : 14 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads