
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Distribute Candies to People in Python
Suppose we want to distribute some number of candies to a row of n people in the following way −
- We then give 1 candy to the first people, 2 candies to the second people, and so on until we give n candies to the last people.
- After that, we go back to the start of the row again, give n + 1 candies to the first people, n + 2 candies to the second people, and so on until we give 2 * n candies to the last people.
We will repeat this process until we run out of candies. The last people will get all of our remaining candies (not necessarily one more than the previous gift).
We have to return an array that represents the final distribution of candies. So suppose candies are 7, and n = 3, then the output will be [2, 2, 3]. So at first the first person will get 1. the array is [1, 0, 0], second one has got 2, then array is [1, 2, 0], third one has got 3, then array is [1, 2, 3], and finally first one again got 1, so array is [2, 2, 3]
To solve this, we will follow these steps −
- res is an array of n elements, and fill with 0
- index := 0
- while candies > 0
- res[index mod n] := res[index mod n] + min of candies and index + 1
- candies := candies – 1
- index := index + 1
- return res
Example
Let us see the following implementation to get better understanding −
class Solution(object): def distributeCandies(self, candies, num_people): res = [0 for i in range(num_people)] index = 0 while candies>0: res[index%num_people] += min(candies,index+1) candies-=(index+1) index+=1 return res ob1 = Solution() print(ob1.distributeCandies(8, 3))
Input
8 3
Output
[3, 2, 3]
- Related Articles
- Distribute Candies in C++
- Program to count the number of ways to distribute n number of candies in k number of bags in Python
- Program to distribute repeating integers in Python
- C++ program to check whether we can distribute bags so that two friends will get same amount of candies
- Program to count number of ways we can distribute coins to workers in Python
- Find the minimum and maximum amount to buy all N candies in Python
- Program to recover shuffled queue of people in python
- If Ramu has 133 chocolates and he has to distribute all chocolates to 120 children, how will he distribute ?
- Distribute Coins in Binary Tree in C++
- Program to check whether first player can take more candies than other or not in Python
- Program to find minimum number of people to teach in Python
- Program to find hoe many children will get candies while distributing them maintaining the rules in Python
- C++ code to find who cannot give sufficient candies
- Maximum Candies You Can Get from Boxes in C++
- Program to find maximum number of people we can make happy in Python

Advertisements