Given an array **arr[]** of size **N** and an integer K which means there are **N** piles of coins and the **i ^{th}** 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.

**Examples:**

Input:arr[] = {2, 2, 2, 2}, K = 0

Output:0

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

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 and increasing them will add to the operations which we need to minimize. 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); ` ` ` ` ` `// Itereate 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; ` `} ` |

*chevron_right*

*filter_none*

## 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` `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 = ` `1` `; ` ` ` ` ` `// Itereate 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 ` |

*chevron_right*

*filter_none*

## 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 ` `=` `1` `; ` ` ` ` ` `# Itereate 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 ` |

*chevron_right*

*filter_none*

## 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` `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 = 1; ` ` ` ` ` `// Itereate 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 */` |

*chevron_right*

*filter_none*

**Output:**

2

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.

## Recommended Posts:

- Find the winner of a game of removing any number of stones from the least indexed non-empty pile from given N piles
- Find maximum number of elements such that their absolute difference is less than or equal to 1
- Longest subarray in which absolute difference between any two element is not greater than X
- Longest subarray with absolute difference between elements less than or equal to K using Heaps
- Triplets in array with absolute difference less than k
- Count of numbers upto N having absolute difference of at most K between any two adjacent digits
- Count of Numbers such that difference between the number and sum of its digits not less than L
- Number of sub-sequence such that it has one consecutive element with difference less than or equal to 1
- Remove elements that appear strictly less than k times
- Sum of all array elements less than X and greater than Y for Q queries
- Remove minimum elements from either side such that 2*min becomes more than max
- Remove minimum elements from either side such that 2*min becomes more than max | Set 2
- Remove minimum elements from the array such that 2*min becomes more than max
- Missing occurrences of a number in an array such that maximum absolute difference of adjacent elements is minimum
- Check if array can be divided into two sub-arrays such that their absolute difference is K
- Count of integers of length N and value less than K such that they contain digits only from the given set
- Maximum product from array such that frequency sum of all repeating elements in product is less than or equal to 2 * k
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Arrange N elements in circular fashion such that all elements are strictly less than sum of adjacent elements
- Maximum length L such that the sum of all subarrays of length L is less than K

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.