Shop in Candy Store | Practice | GeeksforGeeks

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.
You are now provided with an attractive offer.
You can buy a single candy from the store and get at most K other candies ( all are different types ) for free.
Now you have to answer two questions. Firstly, you have to find what is the minimum amount of money you have to spend to buy all the N different candies. Secondly, you have to find what is the maximum amount of money you have to spend to buy all the N different candies.
In both the cases you must utilize the offer i.e. you buy one candy and get K other candies for free.
Example 1:
Input:
N = 4
K = 2
candies[] = {3 2 1 4}

Output:
3 7

Explanation:
As according to the offer if you buy 
one candy you can take at most two 
more for free. So in the first case, 
you buy the candy which costs 1 and 
takes candies worth 3 and 4 for free, 
also you buy candy worth 2 as well.
So min cost : 1+2 =3.
In the second case, you can buy the 
candy which costs 4 and takes candies 
worth 1 and 2 for free, also you need 
to buy candy worth 3 as well. 
So max cost : 3+4 =7.
Example 2:
Input: 
N = 5
K = 4
candies[] = {3 2 1 4 5}

Output: 
1 5

Explanation:
For minimimum cost buy the candy with
the cost 1 and get all the other candies
for free.
For maximum cost buy the candy with
the cost 5 and get all other candies
for free.

Your Task:  
You don't need to read input or print anything. Your task is to complete the function candyStore() which takes the array candies[], its size N and an integer K as input parameters and returns the minimum amount and maximum amount of money to buy all candies according to the offer.

Expected Time Complexity: O(NLogN)
Expected Auxiliary Space: O(1)

Constraints:
1 <= N <= 100000
 0 <= K <= N-1
1 <= candies[i] <= 10000
0
vikingwarriorin 11 hours
Python Code:
 
def candyStore(self,candies,N,K):
   
        candies = sorted(candies)
        countOfPurchase = (N//(K+1))
        if N % (K+1) != 0:
            countOfPurchase += 1
            
            
        return sum(candies[:countOfPurchase]), sum(candies[-countOfPurchase:])
0
vikingwarrior
This comment was deleted.
0
chiyan sethuin 7 hours
import math

class Solution:
    def candyStore(self, candies, n, k):
        candies.sort()
        cnt = math.ceil(n / (k + 1))
        return sum(candies[:cnt]), sum(candies[-cnt:])
0
sikkusaurav12315 hours ago
public:   vector<int> candyStore(int candies[], int N, int K)   {       // Write Your Code here       sort(candies,candies+N);       int dif_can=0;       int price=0;       vector<int>v;              for(int i=0;i<N;i++)       {           price=price+candies[i];           dif_can=dif_can+K+1;           if(dif_can>=N)           {               v.push_back(price);               break;           }       }       reverse(candies,candies+N);        dif_can=0;        price=0;        for(int i=0;i<N;i++)       {           price=price+candies[i];           dif_can=dif_can+K+1;           if(dif_can>=N)           {               v.push_back(price);               break;           }       }       return v;   }
 
0
subrotomukherjee816 hours ago
def candyStore(self, a,n,k):
        # code here
        mn,mx=0,0
        a.sort()
        val=n//(k+1)
        if n%(k+1)!=0:
            val+=1
        for i in range(0,n):
            if i<val:
                mn+=a[i]
        a=a[::-1]
        for i in range(n):
            if i<val:
                mx+=a[i]
        return mn,mx
0
45mzzbhoest5k5060ff1se8zbxbntzof9sjy5d1e19 hours ago
class Solution{public:   vector<int> candyStore(int candies[], int N, int K)   {       // Write Your Code here              int mincost=0,maxcost=0;       int mx=N-1, mi=0;       sort(candies,candies+N);       for(int i=0,j=N-1;i<=mx && j>=mi;i++,j--){           mincost+=candies[i];           maxcost+=candies[j];           mx -=K;           mi +=K;       }       return {mincost,maxcost};   }};
+1
gourangp2923 hours ago
vector<int> candyStore(int candies[], int N, int K)   {       // Write Your Code here       int minamt=0;       int maxamt=0;       sort(candies,candies+N);       int x=0;       for(int i=0; i<N-x; i++)       {           minamt+=candies[i];           x = x+K;       }       int y=0;       for(int i=N-1; i>=y; i--)       {           maxamt+=candies[i];           y = y+K;       }       return {minamt,maxamt};   }
0
praveenkumar122 days ago
May be it's a good approach.
But Looking for more optimal!
public:
    vector<int> candyStore(int candies[], int N, int K)
    {
        // Write Your Code here
        vector<int>ret;
        sort(candies,candies+N);
        int remains = N-1;
        int i=0;
        int min=0;
        while(i<=remains){
            min+=candies[i];
            remains-=K;
            i++;
        }
        ret.push_back(min);
        
        reverse(candies,candies+N);
        
        remains = N-1;
        int j=0;
        int max=0;
        while(j<=remains){
            max+=candies[j];
            remains-=K;
            j++;
        }
        ret.push_back(max);
        
        return ret;
    }
+1
yashchawla1162 days ago
Simple To Understand And Easy To Implement.
 
https://yashboss116.blogspot.com/2022/04/shop-in-candy-store-geeks-for-geeks.html
 
Approaches Explained And Complexities Mentioned.
 
 
0
jaishp32 days ago
vector<int> candyStore(int candies[], int N, int K)   {       // Write Your Code here       sort(candies,candies+N);       int t1=0,t2=0,i=0,min=0,max=0;       while(N>t1){           min+=candies[i];           i++;           t1+=(K+1);       }       i=N-1;       while(N>t2){           max+=candies[i];           i--;           t2+=(K+1);       }       return{min,max};   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.