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,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,l , the number of test cases.
The next t lines each contain 3 space-separated integers:
- n: the number of prisoners
- m: the number of sweets
- s: the chair number to start passing out treats at
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
Save the Prisoner! HackerRank Solution in C
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> int main() { int t, n, m, s; scanf("%d", &t); while (t--) scanf("%d%d%d", &n, &m, &s), printf("%d\n", (m+s-2)%n+1); return 0; }
Save the Prisoner! HackerRank Solution in 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; }
Save the Prisoner! HackerRank Solution in Java
import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { Scanner input = new Scanner(System.in); int rounds = input.nextInt(); for(int i = 0; i < rounds; i++) { int num = input.nextInt(); int lop = input.nextInt(); int s = input.nextInt() - 1; while(lop != 0) { lop--; s++; if(s > num) s = 1; } System.out.println(s); } } }
Save the Prisoner! HackerRank Solution in Python
# Enter your code here. Read input from STDIN. Print output to STDOUT x=int(raw_input()) for i in range(x): [N,M,S]=[int(j) for j in raw_input().split()] val= (N+M+S-1)%N if val==0: print N else: print val
Save the Prisoner! HackerRank Solution in C#
using System; using System.Collections.Generic; using System.IO; class Solution { static void Main(String[] args) { var T = Convert.ToInt32(Console.ReadLine()); for (int i = 0; i < T; i++) { var str = Console.ReadLine().Split(' '); var N = Convert.ToInt32(str[0]); var M = Convert.ToInt32(str[1]); var S = Convert.ToInt32(str[2]); var r = (M + S - 1) % N; if (r == 0) r = N; Console.WriteLine(r); } } }
Attempt Save the Prisoner! HackerRank Challenge
Link – https://www.hackerrank.com/challenges/save-the-prisoner/
Next HackerRank Challenge Solution
Link – https://exploringbits.com/circular-array-rotation-hackerrank-solution/
Aayush Kumar Gupta is the founder and creator of ExploringBits, a website dedicated to providing useful content for people passionate about Engineering and Technology. Aayush has completed his Bachelor of Technology (Computer Science & Engineering) from 2018-2022. From July 2022, Aayush has been working as a full-time Devops Engineer.