Skip to content

Commit 83c4df7

Browse files
solves save the prisoners
1 parent 9df695c commit 83c4df7

File tree

3 files changed

+23
-61
lines changed

3 files changed

+23
-61
lines changed

src/ViralAdvertising.java

-29
This file was deleted.
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// https://www.hackerrank.com/challenges/save-the-prisoner/problem
2+
3+
package implimentation;
4+
5+
import java.util.Scanner;
6+
7+
public class SaveThePrisoner {
8+
public static void main(String[] args) {
9+
Scanner scanner = new Scanner(System.in);
10+
int queries = scanner.nextInt();
11+
while (queries-- > 0) {
12+
long prisoners = scanner.nextLong();
13+
long sweets = scanner.nextLong();
14+
long startIndex = scanner.nextLong();
15+
System.out.println(unluckyPrisoner(prisoners, sweets, startIndex));
16+
}
17+
}
18+
19+
private static long unluckyPrisoner(long prisoners, long sweets, long startIndex) {
20+
return ((startIndex - 1) + sweets % prisoners - 1 + prisoners) % prisoners + 1;
21+
}
22+
}

src/test.java

+1-32
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,7 @@ public class test {
44
private static final Scanner scanner = new Scanner(System.in);
55

66
public static void main(String[] args) {
7-
int length = scanner.nextInt();
8-
int[] array = getArray(length);
9-
int element = scanner.nextInt();
10-
System.out.println(search(array, element));
11-
}
12-
13-
// Binary Search that will return the exact or nearest smallest value if absent
14-
// and -1 of item not found and no smaller value available
15-
private static int search(int[] array, int element) {
16-
int mid = array.length / 2;
17-
for (int start = 0, end = array.length ; start <= end && mid < array.length && mid >= 0 ; mid = (start + end) / 2) {
18-
if (array[mid] == element) {
19-
return mid;
20-
} else if (array[mid] < element) {
21-
start = mid + 1;
22-
} else {
23-
end = mid - 1;
24-
}
25-
}
26-
27-
if (check(array, mid + 1, element)) {
28-
return mid + 1;
29-
} else if (check(array, mid, element)) {
30-
return mid;
31-
} else if (check(array, mid - 1, element)) {
32-
return mid - 1;
33-
}
34-
return -1;
35-
}
36-
37-
private static boolean check(int[] array, int index, int element) {
38-
return index - 1 >= 0 && index - 1 < array.length && array[index] < element;
7+
System.out.println(-1 % 2);
398
}
409

4110
private static int[] getArray(int length) {

0 commit comments

Comments
 (0)