Skip to content

Commit 26770fd

Browse files
committed
add problem Coin Piles
1 parent ac9aa0f commit 26770fd

File tree

2 files changed

+94
-0
lines changed

2 files changed

+94
-0
lines changed

coin-piles/problem_statement.txt

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
Coin Piles
2+
=======================
3+
There are N piles of coins each containing Ai (1<=i<=N) coins.
4+
Now, you have to adjust the number of coins in each pile such that for any two pile, if a is the number of coins in first pile and b is the number of coins in second pile then |a-b|<= K.
5+
In order to do that you can remove coins from different piles to decrease the number of coins in those piles but you cannot increase the number of coins in a pile by adding more coins. Now, given a value of N and K, along with the sizes of the N different piles you have to tell the minimum number of coins to be removed in order to satisfy the given condition.
6+
7+
Note: You can also remove a pile by removing all the coins of that pile.
8+
9+
Input
10+
=====
11+
12+
The first line of the input contains T, the number of test cases. Then T lines follow. Each test case contains two lines. The first line of a test case contains N and K. The second line of the test case contains N integers describing the number of coins in the N piles.
13+
14+
Output
15+
=====
16+
17+
For each test case output a single integer containing the minimum number of coins needed to be removed in a new line.
18+
19+
Constraints:
20+
1<=T<=50
21+
1<=N<=100
22+
1<=Ai<=1000
23+
0<=K<=1000
24+
25+
Example
26+
=====
27+
28+
Input
29+
3
30+
4 0
31+
2 2 2 2
32+
6 3
33+
1 2 5 1 1 1
34+
6 3
35+
1 5 1 2 5 1
36+
37+
Output
38+
0
39+
1
40+
2
41+
42+
Explanation:
43+
44+
1. In the first test case, for any two piles the difference in the number of coins is <=0. So no need to remove any coins.
45+
2. In the second test case if we remove one coin from pile containing 5 coins then for any two piles the absolute difference in the number of coins is <=3.
46+
3. In the third test case 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.

coin-piles/solution.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int main() {
5+
int t,n,k,a[100],d[100],rem[100],low,hi,removals,d_idx,min_removals;
6+
unordered_map<int,int>freq;
7+
8+
scanf("%d\n", &t);
9+
while(t--) {
10+
scanf("%d %d\n", &n, &k);
11+
for(int lc = 0; lc < n; lc++) {
12+
scanf("%d" , &a[lc]);
13+
}
14+
15+
sort(a,a + n);
16+
d_idx=0;
17+
freq.clear();
18+
for(int lc = 0; lc < n; lc++) {
19+
int key = a[lc], cnt = 0;
20+
while(lc < n && a[lc] == key)
21+
lc++;
22+
lc--;
23+
d[d_idx] = key;
24+
d_idx++;
25+
}
26+
27+
low = 0;
28+
min_removals = INT_MAX;
29+
while(low < d_idx) {
30+
int cur = d[low];
31+
removals = 0;
32+
for(int lc = 0; lc < n; lc++) {
33+
if(a[lc] < cur)
34+
rem[lc] = a[lc];
35+
else if(a[lc] >= cur && a[lc] <= (cur + k))
36+
rem[lc] = 0;
37+
else if(a[lc] > (cur + k))
38+
rem[lc] = (a[lc] - (cur + k));
39+
40+
removals += rem[lc];
41+
}
42+
min_removals = min(min_removals, removals);
43+
low++;
44+
}
45+
printf("%d\n",min_removals);
46+
}
47+
return 0;
48+
}

0 commit comments

Comments
 (0)