Geeksforgeeks

X

DAYS

:

HOUR

:

MINS

:

SEC

Error

Copied to Clipboard

There are **N** piles of coins each containing **Ai** (1<=i<=N) coins. Find the minimum number of coins to be removed such that the absolute difference of coins in any two piles is at most **K.**

**Note**: You can also remove a pile by removing all the coins of that pile.

**Example 1:**

Input:N = 4, K = 0 arr[] = {2, 2, 2, 2}Output:0Explanation:For any two piles the difference in the number of coins is <=0. So no need to remove any coins.

Input:N = 6, K = 3 arr[] = {1, 5, 1, 2, 5, 1}Output :2Explanation: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.

**Your Task: **

You don't need to read input or print anything. Your task is to complete the function **minSteps()** which takes 2 integers N, and K and an array A of size N as input and returns the minimum number of coins that need to be removed.

**Expected Time Complexity:** O(N*logN)

**Expected Auxiliary Space:** O(N)

**Constraints:**

1 ≤ N ≤ 10^{5}

0 ≤ K ≤ 10^{3}

1 ≤ A[i] ≤ 10^{3}

We are replacing the old Disqus forum with the new Discussions section given below.

Click here to view old Disqus comments.

Click here to view old Disqus comments.

Login to report an issue on this page.

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

YesLoading...

Coin Piles

...