Created
December 27, 2019 06:08
-
-
Save AnkurVyas-BTC/3c3506d28ebd41e060358870530bbd47 to your computer and use it in GitHub Desktop.
Distribute N candies among K children
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
candies = 10 | |
no_of_children = 3 | |
children = Array.new(no_of_children, 0) # [0,0,0, ...n] | |
main_index = 0 | |
while(candies > 0) | |
if candies > main_index | |
children[main_index % no_of_children] += main_index + 1 | |
candies = candies - (main_index + 1) | |
else | |
children[main_index % no_of_children] += candies | |
candies = 0 | |
end | |
main_index = main_index + 1 | |
end | |
children |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment