Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike

1 Answer

0 like 0 dislike
class Solution {
public:
    vector<int> distributeCandies(int candies, int num_people) {
       vector<int> answer(num_people,0);
       int i=0;
       int count=1;
       while(candies!=0){
           if(candies<=count){ // Checking if we have less candies than required
               answer[i]+=candies;
               break;
           }
           answer[i]+=count;
           candies-=count;
           count++;
           i++;
           if(i>num_people-1)i=0; // starting again
           
       }
       return answer;
    }
};
by (300 points)