Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programmingoneonone
Programmingoneonone

HackerRank Save the Prisoner! problem solution

YASH PAL, 31 July 202430 November 2025

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

Save the Prisoner! solution in Python.

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;
            next=(next-1)%N;
            if(next == 0)next = N;
            System.out.println(next);
        }
    }
}

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() {
#ifdef NEREVAR_PROJECT
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    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",&t);
    while(t--)
        {
        scanf("%ld %ld %ld",&n,&m,&s);
        tot=m+s-1;
        if(tot%n==0)
            printf("%ldn",n);
        else printf("%ldn",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;
        }

        console.log(last);
    }
} 

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

process.stdin.on("end", function () {
   processData(_input);
});

Algorithms coding problems solutions AlgorithmsHackerRank

Post navigation

Previous post
Next post

Leave a Reply

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

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes