Skip to content

Commit fb40ead

Browse files
authored
Add "shop in candy store" (blakeembrey#191)
1 parent a2c4480 commit fb40ead

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Shop in Candy Store
2+
3+
In a candy store there are N different types of candies available and the prices of all the N different types of candies are provided to you.
4+
You are now provided with an attractive offer.
5+
You can buy a single candy from the store and get atmost K other candies ( all are different types ) for free.
6+
Now you have to answer two questions. Firstly, you have to tell what is the minimum amount of money you have to spend to buy all the N different candies. Secondly, you have to tell what is the maximum amount of money you have to spend to buy all the N different candies.
7+
In both the cases you must utilize the offer i.e. you buy one candy and get K other candies for free.
8+
9+
10+
# Input
11+
The first line of the input contains T the number of test cases. Each test case consists of two lines. The first line of each test case contains the values of N and K as described above. Then in the next line N integers follow denoting the price of each of the N different candies.
12+
13+
14+
# Output
15+
For each test case output a single line containing 2 space separated integers , the first denoting the minimum amount of money required to be spent and the second denoting the maximum amount of money to be spent.
16+
Remember to output the answer of each test case in a new line.
17+
18+
# Constraints
19+
1 <= T <= 50
20+
1 <= N <= 1000
21+
0 <= K <= N-1
22+
1 <= Ai <= 100
23+
24+
# Example:
25+
Input
26+
1
27+
4 2
28+
3 2 1 4
29+
30+
Output
31+
3 7
32+
33+
# Explanation
34+
As according to the offer if you but one candy you can take atmost two more for free.
35+
So in the first case you buy the candy which costs 1 and take candies worth 3 and 4 for free, also you buy candy worth 2 as well.
36+
So min cost = 1+2 =3.
37+
In the second case I buy the candy which costs 4 and take candies worth 1 and 2 for free, also I buy candy worth 3 as well.
38+
So max cost = 3+4 =7.
39+
40+
# Topics :
41+
Greedy || Sorting

solutions/cpp/ShopInCandyStore.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
int main()
4+
{
5+
int t; cin>>t;
6+
while(t--)
7+
{
8+
int n,k; cin>>n>>k; long int a[n],min=0,max=0,c=0;
9+
for(int i=0;i<n;i++) cin>>a[i];
10+
sort(a,a+n);
11+
int x=(n/(k+1)); if((n%(k+1))>0) {x++;}
12+
13+
for(int i=0;i<x;i++) {min+=a[i];}
14+
for(int i=n-1;c<x;i--) {max+=a[i];c++;}
15+
cout<<min<<" "<<max<<endl;
16+
}
17+
return 0;
18+
}

0 commit comments

Comments
 (0)