HackerRank Save the Prisoner Solution

Hello Programmers, In this post, you will know how to solve the HackerRank Save the Prisoner Solution. This problem is a part of the HackerRank Algorithms Series.

HackerRank Save the Prisoner Solution
HackerRank Save the Prisoner Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

HackerRank Save the Prisoner Solution

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 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 <= 109
  • 1 <= m <= 109
  • 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 m = 2 sweets. Distribution starts at seat number s = 1. Prisoners in seats numbered 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, m = 19 sweets and they are passed out starting at chair s = 2. The candies go all around twice and there are 5 more candies passed to each prisoner from seat 2 to seat 6.

In the second test case, there are n = 3 prisoners, m = 7 candies and they are passed out starting at seat s = 3. They go around twice, and there is one more to pass out to the prisoner at seat 3.

HackerRank Save the Prisoner Solution

Save the Prisoner 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 Solution in Cpp

#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 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);
        }    
    }
}
Ezoicreport this ad

Save the Prisoner Solution in Python

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 Solution using JavaScript

process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
    input_stdin += data;
});
process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});
function readLine() {
    return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
    var T = readLine();
    
    while(T--) {
        var P = readLine().split(' ');
        
        var N = +P[0]-1;
        var M = +P[1]-1;
        var S = +P[2]-1;
        
        //S=4;
        // 0 1 2 3 4
        // 1 2
        //console.log('N',N,'M',M,'S',S);
        
        if (S+M<N) {
            console.log(S+M+1);
        } else {
            console.log((  (S+M)%(N+1) ) + 1 );
        };
        
        
    };
    
}

Save the Prisoner Solution in Scala

object Solution {
  def main(args: Array[String]) {
    val input = readInput
    for (c <- input) {
      println(poisoned(c._1, c._2, c._3))
    }
  }
  def poisoned(n: Int, toDist: Int, s: Int): Int = {
    var m = toDist
    while (m > n)
      m -= n
    if (s + m <= n + 1) s + m - 1
    else m - (n - s + 1)
  }
  def readInput: (List[(Int, Int, Int)]) = {
    val sc = new java.util.Scanner(System.in)
    val t = sc.nextInt()
    var list: List[(Int, Int, Int)] = Nil
    for (i <- 0 until t) {
      list = list ++ List((sc.nextInt(), sc.nextInt(), sc.nextInt()))
    }
    list
  }
}

Save the Prisoner Solution in Pascal

var N,M,S,i,T:longint;
R:array[-1..104] of longint;
begin
readln(t);
for i:=1 to T do
begin
readln(n,m,s);
r[i]:=(m+s-1) mod n;
if r[i]=0 then r[i]:=n;
end;
for i:=1 to T do writeln(R[i]);
end.

Disclaimer: This problem (Save the Prisoner) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: HackerRank Viral Advertising Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad