-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_ShopInCandyStore.cpp
62 lines (54 loc) · 1.08 KB
/
GFG_ShopInCandyStore.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
https://practice.geeksforgeeks.org/problems/shop-in-candy-store1145/1
Shop in Candy Store
*/
// { Driver Code Starts
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution
{
public:
vector<int> candyStore(int candies[], int N, int K)
{
int minc=0;
int maxc=0;
sort(candies, candies+N);
int i=0;
int j=N-1;
while(i<=j)
{
minc += candies[i];
i++;
j = j-K;
}
i=0; j=N-1;
while(i<=j)
{
maxc += candies[j];
j--;
i = i+K;
}
return {minc, maxc};
}
};
// { Driver Code Starts.
int main()
{
int t;
cin >> t;
while (t--)
{
int N, K;
cin >> N >> K;
int candies[N];
for (int i = 0; i < N; i++)
{
cin >> candies[i];
}
Solution ob;
vector<int> cost = ob.candyStore(candies, N, K);
cout << cost[0] << " " << cost[1] << endl;
}
return 0;
} // } Driver Code Ends