Solution to Chocolates-By-Numbers by codility

17 Feb

Question: https://codility.com/demo/take-sample-test/chocolates_by_numbers

Question Name: Chocolates-By-Numbers or ChocolatesByNumbers

When we met with an empty wrapper, we must have been this position for twice. We use i for the first time and j for the second time. Due to the modulo feature, there must be nature number, to say k, so that: i * M + k * N = j * M. Then we could easily prove that the smallest (earliest) i must be zero (for all i != 0, then (i-i) * M + k * N = (j-i) * M ). So the first eaten position would be first position that you meet again. Finally, the j would be the number of chocolates that you will eat.

2 thoughts on “Solution to Chocolates-By-Numbers by codility

Leave a Reply

Your email address will not be published. Required fields are marked *