📌  相关文章
📜  移除最小硬币,使得任意两堆之间的绝对差小于 K

📅  最后修改于: 2022-05-13 01:57:46.728000             🧑  作者: Mango

移除最小硬币,使得任意两堆之间的绝对差小于 K

给定一个数组,大小为Narr[]和一个整数 K,这意味着有N堆硬币,第i包含arr[i]个硬币。任务是调整每堆硬币的数量,使得对于任何两堆硬币,如果a是第一堆硬币的数量, b是第二堆硬币的数量,那么|a – b| ≤ 钾
人们可以从不同的堆中取出硬币以减少这些堆中的硬币数量,但不能通过添加更多硬币来增加一堆中的硬币数量。找出满足给定条件的最小硬币数量。

例子:

方法:因为我们不能增加一堆硬币的数量。因此,任何堆中的最小硬币数量将保持不变,因为它们不能被移除,并且增加它们将增加我们需要最小化的操作。现在,找到一堆中的最小硬币,如果当前堆中的硬币与最小硬币堆中的硬币之间的差大于 K,则每隔一堆从当前堆中取出多余的硬币。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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


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


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


输出
2