-
Notifications
You must be signed in to change notification settings - Fork 368
/
josephus.java
54 lines (40 loc) · 1.91 KB
/
josephus.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
import java.util.Scanner;
public class josephus {
// Josephus Problem Algorithm:
// 1. Create an array to represent the circle of people, numbered from 1 to n.
// 2. Start with an index variable pointing to the first person.
// 3. Repeat the elimination process until only one person remains:
// a. Move the index k-1 steps in a circular manner.
// b. Remove the person at the current index by shifting the remaining elements.
// c. Update the index to the next position after the elimination, wrapping around if necessary.
// 4. Return the value of the last remaining person.
public static int JosephusProblem(int n, int k) {
int[] people = new int[n];
// Initialize the array with people numbered from 1 to n
for (int i = 0; i < n; i++) {
people[i] = i + 1;
}
int index = 0;
// Elimination process
while (n > 1) {
// Move the index k-1 steps in a circular manner
index = (index + k - 1) % n;
// Remove the person at the current index by shifting the remaining elements
for (int i = index; i < n - 1; i++) {
people[i] = people[i + 1];
}
n--;
}
return people[0]; // Return the value of the last remaining person
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of people in the circle: ");
int n = scanner.nextInt();
System.out.print("Enter the elimination step size: ");
int k = scanner.nextInt();
int lastPerson = JosephusProblem(n, k); // Solve the Josephus Problem
System.out.println("The last remaining person is at position: " + lastPerson);
scanner.close();
}
}