0% found this document useful (0 votes)
15 views

Save The Prisoners: Import Import Public Class Public Static Void New Int For Int Int Int Int Int If

The document describes a Java program that takes in prisoner and poison inputs and calculates which prisoner would be poisoned based on the inputs and using modulo arithmetic. It notes there are two possible implementations shown and that the second implementation improves the time limit for some test cases.

Uploaded by

Charitha Iddum
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Save The Prisoners: Import Import Public Class Public Static Void New Int For Int Int Int Int Int If

The document describes a Java program that takes in prisoner and poison inputs and calculates which prisoner would be poisoned based on the inputs and using modulo arithmetic. It notes there are two possible implementations shown and that the second implementation improves the time limit for some test cases.

Uploaded by

Charitha Iddum
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Save the prisoners:

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

public class Solution {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        int T = input.nextInt();
        
        for(int i = 0; i < T; i++)
        {
            int N = input.nextInt();
            int M = input.nextInt();
            int S = input.nextInt();
            
            int poisonedPrisoner = (S + M - 1) % N;
            if(poisonedPrisoner == 0){poisonedPrisoner=N;}
            System.out.println(poisonedPrisoner);
        }
    }
}
(or)
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);
}
}
}
Time limit exceeded for some test cases.

You might also like