Skip to content

Instantly share code, notes, and snippets.

@AnkurVyas-BTC
Created December 27, 2019 06:08
Show Gist options
  • Save AnkurVyas-BTC/3c3506d28ebd41e060358870530bbd47 to your computer and use it in GitHub Desktop.
Save AnkurVyas-BTC/3c3506d28ebd41e060358870530bbd47 to your computer and use it in GitHub Desktop.
Distribute N candies among K children
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