Open In App

Remove Minimum coins such that absolute difference between any two piles is less than K

Improve
Improve
Like Article
Like
Save
Share
Report

Given an array, arr[] of size N and an integer K which means there are N piles of coins and the ith contains arr[i] coins. The task is to adjust the number of coins in each pile such that for any two piles if a be the number of coins in the first pile and b be the number of coins in the second pile then |a – b| <= K
One can remove coins from different piles to decrease the number of coins in those piles but cannot increase the number of coins in a pile by adding more coins. Find the minimum number of coins to be removed in order to satisfy the given condition.

Please note that we cannot remove the whole pile. In other words a pile with 0 coins is also considered considering differences.

Examples: 

Input: arr[] = {2, 2, 2, 2}, K = 0 
Output:
For any two piles the difference in the number of coins is <= 0. 
So, no need to remove any coins.
Input: arr[] = {1, 5, 1, 2, 5, 1}, K = 3 
Output:
If we remove one coin each from both the piles containing 
5 coins, then for any two piles the absolute difference 
in the number of coins is <= 3. 

Approach: Since we cannot increase the number of coins in a pile. So, the minimum number of coins in any pile will remain the same as they can’t be removed. Now, find the minimum coins in a pile, and for every other pile if the difference between the coins in the current pile and the minimum coin pile is greater than K then remove the extra coins from the current pile.

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 number
// of coins that need to be removed
int minimumCoins(int a[], int n, int k)
{
    // To store the coins needed to be removed
    int cnt = 0;
 
    // Minimum value from the array
    int minVal = *min_element(a, a + n);
 
    // Iterate over the array
    // and remove extra coins
    for (int i = 0; i < n; i++)
    {
        int diff = a[i] - minVal;
 
        // If the difference between
        //  the current pile and the
        // minimum coin pile is greater than k
        if (diff > k)
        {
 
            // Count the extra coins to be removed
            cnt += (diff - k);
        }
    }
 
    // Return the required count
    return cnt;
}
 
// Driver code
int main()
{
    int a[] = { 1, 5, 1, 2, 5, 1 };
    int n = sizeof(a) / sizeof(a[0]);
    int k = 3;
 
    cout << minimumCoins(a, n, k);
 
    return 0;
}


Java




// Java implementation of the approach
import java.io.*;
 
class GFG {
 
    // Function to return the minimum number
    // of coins that need to be removed
    static int min_val(int[] a)
    {
        int min = 2147483647;
        for (int el : a) {
            if (el < min) {
                min = el;
            }
        }
        return min;
    }
    static int minimumCoins(int a[], int n, int k)
    {
        // To store the coins needed to be removed
        int cnt = 0;
 
        // Minimum value from the array
        int minVal = min_val(a);
 
        // Iterate over the array and remove extra coins
        for (int i = 0; i < n; i++) {
            int diff = a[i] - minVal;
 
            // If the difference between the current pile
            // and the minimum coin pile is greater than k
            if (diff > k) {
                // Count the extra coins to be removed
                cnt += (diff - k);
            }
        }
 
        // Return the required count
        return cnt;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int a[] = { 1, 5, 1, 2, 5, 1 };
        int n = a.length;
        int k = 3;
        System.out.println(minimumCoins(a, n, k));
    }
}
 
// This code is contributed by jit_t


C#




// C# implementation of the approach
using System;
 
class GFG {
   
   
    // Function to return the minimum number
    // of coins that need to be removed
    static int min_val(int[] a, int n)
    {
        int min = 2147483647;
        for (int i = 0;i<n;i++)
        {
            int el = a[i];
            if (el < min)
            {
                min = el;
            }
        }
        return min;
    }
 
    // Function to return the minimum number
    // of coins that need to be removed
    static int minimumCoins(int[] a, int n, int k)
    {
        // To store the coins needed to be removed
        int cnt = 0;
 
        // Minimum value from the array
        int minVal = min_val(a, n);
 
        // Iterate over the array and remove extra coins
        for (int i = 0; i < n; i++)
        {
            int diff = a[i] - minVal;
 
            // If the difference between the current pile
            // and the minimum coin pile is greater than k
            if (diff > k)
            {
                // Count the extra coins to be removed
                cnt += (diff - k);
            }
        }
 
        // Return the required count
        return cnt;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int[] a = { 1, 5, 1, 2, 5, 1 };
        int n = a.Length;
        int k = 3;
        Console.WriteLine(minimumCoins(a, n, k));
    }
}
 
/* This code is contributed by PrinciRaj1992 */


Javascript




<script>
 
    // Javascript implementation of the approach
     
    // Function to return the minimum number
    // of coins that need to be removed
    function min_val(a, n)
    {
        let min = 2147483647;
        for (let i = 0;i<n;i++)
        {
            let el = a[i];
            if (el < min)
            {
                min = el;
            }
        }
        return min;
    }
  
    // Function to return the minimum number
    // of coins that need to be removed
    function minimumCoins(a, n, k)
    {
        // To store the coins needed to be removed
        let cnt = 0;
  
        // Minimum value from the array
        let minVal = min_val(a, n);
  
        // Iterate over the array and remove extra coins
        for (let i = 0; i < n; i++)
        {
            let diff = a[i] - minVal;
  
            // If the difference between the current pile
            // and the minimum coin pile is greater than k
            if (diff > k)
            {
                // Count the extra coins to be removed
                cnt += (diff - k);
            }
        }
  
        // Return the required count
        return cnt;
    }
     
    let a = [ 1, 5, 1, 2, 5, 1 ];
    let n = a.length;
    let k = 3;
    document.write(minimumCoins(a, n, k));
 
</script>


Python3




# Python implementation of the approach
 
# Function to return the minimum number
# of coins that need to be removed
def minimumCoins(a, n, k):
    # To store the coins needed to be removed
    cnt = 0;
 
    # Minimum value from the array
    minVal = min(a);
 
    # Iterate over the array and remove extra coins
    for i in range(n):
        diff = a[i] - minVal;
 
        # If the difference between the current pile and
        # the minimum coin pile is greater than k
        if (diff > k):
            # Count the extra coins to be removed
            cnt += (diff - k);
    # Return the required count
    return cnt;
 
# Driver code
a = [1, 5, 1, 2, 5, 1];
n = len(a);
k = 3;
print(minimumCoins(a, n, k));
 
     
# This code is contributed by 29AjayKumar


Output

2

Time Complexity: O(n), where n is the size of the array
Auxiliary Space: O(1), as no extra space is used



Last Updated : 06 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads