In this post, we will solve **Save the Prisoner! HackerRank Solution**. This problem **(Save the Prisoner!)** is a part of **HackerRank Algorithms** series.

**Task**

A jail has a number of prisoners and a number of treats to pass out to them. Their jailer decides the fairest way to divide the treats is to seat the prisoners around a circular table in sequentially numbered chairs. A chair number will be drawn from a hat. Beginning with the prisoner in that chair, one candy will be handed to each prisoner sequentially around the table until all have been distributed.

The jailer is playing a little joke, though. The last piece of candy looks like all the others, but it tastes *awful*. Determine the chair number occupied by the prisoner who will receive that candy.

**Example**

*n* = 4*m* = 6*s* = 2

There are **4** prisoners, **6** pieces of candy and distribution starts at chair **2**. The prisoners arrange themselves in seats numbered **1** to **4**. Prisoners receive candy at positions **2, 3, 4, 1, 2, 3**. The prisoner to be warned sits in chair number **3**.

**Function Description**

Complete the *saveThePrisoner* function in the editor below. It should return an integer representing the chair number of the prisoner to warn.

saveThePrisoner has the following parameter(s):

*int n*: the number of prisoners*int m*: the number of sweets*int s*: the chair number to begin passing out sweets from

**Returns**

*int:*the chair number of the prisoner to warn

**Input Format**

The first line contains an integer, * t*, the number of test cases.

The next

*lines each contain*

**t****3**space-separated integers:

: the number of prisoners**n**: the number of sweets**m**: the chair number to start passing out treats at**s**

**Constraints**

**1 <=***t*<= 100**1 <=***n*<= 10^{9}**1 <=***m*<= 10^{9}**1 <=***s*<=*n*

**Sample Input 0**

```
2
5 2 1
5 2 2
```

**Sample Output 0**

```
2
3
```

**Explanation 0**

In the first query, there are** n = 5** prisoners and

**sweets. Distribution starts at seat number**

*m*= 2**. Prisoners in seats numbered**

*s*= 1**1**and

**2**get sweets. Warn prisoner

**2**.

In the second query, distribution starts at seat **2** so prisoners in seats **2** and **3** get sweets. Warn prisoner **3**.

**Sample Input 1**

```
2
7 19 2
3 7 3
```

**Sample Output 1**

```
6
3
```

**Explanation 1**

In the first test case, there are ** n = 7** prisoners,

**sweets and they are passed out starting at chair**

*m*= 19**. The candies go all around twice and there are**

*s*= 2**5**more candies passed to each prisoner from seat

**2**to seat

**6**.

In the second test case, there are ** n = 3** prisoners,

**candies and they are passed out starting at seat**

*m*= 7**. They go around twice, and there is one more to pass out to the prisoner at seat**

*s*= 3**3**.

**Solution – Save the Prisoner! – HackerRank Solution**

**C++**

#include <cmath> #include <cstdio> #include <vector> #include <iostream> #include <algorithm> using namespace std; int main() { int t; cin >> t; while(t--) { int n,m,s; cin >> n >> m >> s; --s; --m; s += m; s %= n; s++; cout << s << endl; } return 0; }

**Python**

#!/bin/python3 import sys def saveThePrisoner(n, m, s): res = (s + m-1) % n return res if res != 0 else n t = int(input().strip()) for a0 in range(t): n, m, s = input().strip().split(' ') n, m, s = [int(n), int(m), int(s)] result = saveThePrisoner(n, m, s) print(result)

**Java**

import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int t = scanner.nextInt(); for(int i=0;i<t;i++) { int n = scanner.nextInt(); int m = scanner.nextInt(); int s = scanner.nextInt(); int result = (m + s - 1) % n; if(result == 0) result = n; System.out.println(result); } } }

**Note:** This problem **(Save the Prisoner!)** is generated by **HackerRank** but the solution is provided by **CodingBroz**. This tutorial is only for **Educational** and **Learning** purpose.

**Also Read:**