Geeksforgeeks

Error

×

Leaderboard

Showing:

Handle | Score |
---|---|

@Ibrahim Nash | 6420 |

@blackshadows | 6380 |

@mb1973 | 5704 |

@Quandray | 5245 |

@akhayrutdinov | 5111 |

@saiujwal13083 | 5046 |

@sanjay05 | 3762 |

@kirtidee18 | 3673 |

@marius_valentin_dragoi | 3523 |

@mantu_singh | 3510 |

@sushant_a | 3459 |

Complete Leaderboard | |

Handle | Score |

@cfwong8 | 1134 |

@gurshehzadsingh | 676 |

@nithinreddy3210 | 610 |

@Run_for_life | 570 |

@divyagupta221100 | 557 |

@jha8768 | 553 |

@AkashLahoty | 537 |

@aayushdesai01 | 526 |

@riyakumaribkj | 524 |

@amansinha74458 | 503 |

@dreamerdreamer | 488 |

Complete Leaderboard |

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}

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

...