-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJosephusProblem.java
More file actions
39 lines (36 loc) · 1.44 KB
/
Copy pathJosephusProblem.java
File metadata and controls
39 lines (36 loc) · 1.44 KB
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
package RecursionAndBacktracking;
import java.util.ArrayList;
import java.util.Scanner;
public class JosephusProblem {
public static void solveJosephus(ArrayList<Integer> persons, int k, int swordIndex){
// Base case
if(persons.size() == 1){
System.out.println(persons.get(0));
return;
}
// performing modulo operation because we have access the element in a circular way in the array
// that's why we are using modulo operator for neglecting indexOutOfBound error
swordIndex = (swordIndex + k) % persons.size();
persons.remove(swordIndex); // removing the person which was killed from the persons array
solveJosephus(persons, k, swordIndex);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter t:");
int t = sc.nextInt();
while (t-- > 0) {
System.out.println("Enter N:");
int n = sc.nextInt();
System.out.println("Enter K:");
int k = sc.nextInt();
// storing the numbers of the person
ArrayList<Integer> persons = new ArrayList<>();
for (int i = 0; i < n; i++) {
persons.add(i + 1);
}
// calling function
// pass k-1 because index starts from 0
solveJosephus(persons, k-1, 0);
}
}
}