Hackerrank – Save the Prisoner

Hackerrank – Problem Statement

A description of the problem can be found on Hackerrank.

Solution

(((sweets % prisoners) + startId) % prisoners) - 1

Modulo finds the id of prisoner who gets the last poisoned candy after all circles distribution. Since the starting id S is not from first prisoner we make an offset. Do modulo again because there are cases when sum of last prisoner and the offset is greater then count of all prisoners.

I created solution in:

All solutions are also available on my GitHub.

Java

JavaScript

Scala

Ruby

Leave a Reply

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