Hackerrank – Chocolate Feast

Problem Statement
A description of the problem can be found on Hackerrank.

Solution
Calculate count of chocolates: chocolates = money / price . Count of chocolates is count of wrappers, too. Free chocolates are calculated:

  1. divide chocolates with count of chocolates needed for discount
  2. add solution of the division to chocolates
  3. new wrappers count is: wrappers = wrappers = wrappers - free_choco * discount + free_choco
  4. if wrappers count is greater than discount go to 1.

I created solution in 4 languages:

All solutions are also available on my GitHub.

Scala

Java

JavaScript

Ruby