-
Notifications
You must be signed in to change notification settings - Fork 0
/
SaveThePrisoner.java
64 lines (51 loc) · 1.85 KB
/
SaveThePrisoner.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package br.com.eduardocintra.easy.savetheprisoner;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.stream.IntStream;
class Result {
/*
* Complete the 'saveThePrisoner' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER n
* 2. INTEGER m
* 3. INTEGER s
*/
public static int saveThePrisoner(int n, int m, int s) {
/*
* Please, if this code helps you, leave your star on the repository:
* https://github.com/eduardocintra/hacker-rank-solutions
*/
int lastChair = (s + m - 1) % n;
return lastChair == 0 ? n : lastChair;
}
}
public class SaveThePrisoner {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));
int t = Integer.parseInt(bufferedReader.readLine().trim());
IntStream.range(0, t)
.forEach(
tItr -> {
try {
String[] firstMultipleInput =
bufferedReader.readLine().replaceAll("\\s+$", "").split(" ");
int n = Integer.parseInt(firstMultipleInput[0]);
int m = Integer.parseInt(firstMultipleInput[1]);
int s = Integer.parseInt(firstMultipleInput[2]);
int result = Result.saveThePrisoner(n, m, s);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
bufferedReader.close();
bufferedWriter.close();
}
}