HackerRank Save the Prisoner! problem solution

In this HackerRank Save the Prisoner! problem 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.

HackerRank Save the Prisoner! problem solution

Problem solution in Python programming.

t = int(input())
for _ in range(t):
    parts = list(map(int, input().split(' ')))
    # print('parts', parts)
    print((parts[1] + parts[2] - 2) % parts[0] + 1)

Problem solution in Java Programming.

import java.io.*;
import java.util.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner scan = new Scanner(System.in);
        int c = Integer.parseInt(scan.nextLine());
        for(int i=0;i<c;i++){
            String[] sp = scan.nextLine().split("\\s+");
            int N = Integer.parseInt(sp[0]);
            int M = Integer.parseInt(sp[1]);
            int S = Integer.parseInt(sp[2]);
            int next = M+S;
            if(next == 0)next = N;

Problem solution in C++ programming.

#include <iostream>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <sstream>
#include <cmath>

typedef long long ll;

#define forn(i, n) for (int i = 0; i < (int)(n); i++)
#define forv(i, v) forn(i, v.size())

using namespace std;

void solve() {
    int n, m, s;
    cin >> n >> m >> s;
    cout << 1 + (s - 1 + m - 1) % n << endl;

int main() {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    int tc;
    cin >> tc;
    forn(it, tc) solve();
    return 0;

Problem solution in C programming.

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>

int main() {

    /* Enter your code here. Read input from STDIN. Print output to STDOUT */    
    long int t,n,m,s,tot;
        scanf("%ld %ld %ld",&n,&m,&s);
        else printf("%ld\n",tot%n);
    return 0;

Problem solution in JavaScript programming.

function processData(input) {
    //Enter your code here
    var a = input.split("\n");
    var n = a.shift();
    for(var i = 0; i < n; i++) {
        var b = a[i].split(" ");

        var prisoners = parseInt(b[0]);
        var sweets = parseInt(b[1]);
        var id = parseInt(b[2]);
        var last = 0;

        last = (sweets+id-1)%prisoners;

        if(last === 0) {
           last = prisoners;


_input = "";
process.stdin.on("data", function (input) {
    _input += input;

process.stdin.on("end", function () {

Post a Comment