Make array elements equal with minimum cost

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: 2
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: 1
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 intial value or it will remain even if it is even as intial. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

2

Performance Analysis:

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

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.



Improved By : Yash_R, nidhi_biet

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.