forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJosephus.java
More file actions
82 lines (76 loc) · 2.44 KB
/
Josephus.java
File metadata and controls
82 lines (76 loc) · 2.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
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.thealgorithms.recursion;
/**
* Josephus - Solves the Josephus problem using recursion
*
* The Josephus problem: n people numbered 1..n stand in a circle. Starting from
* position 1, every k-th person is eliminated until one remains. This class
* provides a recursive solution to compute the survivor's position (1-based).
*
* Recurrence (0-based): J(1,k) = 0; J(n,k) = (J(n-1,k) + k) % n
* Convert to 1-based by adding 1.
*
* @author ritesh-3822
* @see <a href="https://en.wikipedia.org/wiki/Josephus_problem">Josephus problem</a>
*/
public final class Josephus {
private Josephus() {
// prevent instantiation
}
/**
* Returns the 1-based position of the survivor for given n and k.
*
* @param n number of people (must be > 0)
* @param k step count (must be > 0)
* @return 1-based index of the survivor
* @throws IllegalArgumentException if n <= 0 or k <= 0
*/
public static int getJosephus(int n, int k) {
if (n <= 0) {
throw new IllegalArgumentException("n must be positive");
}
if (k <= 0) {
throw new IllegalArgumentException("k must be positive");
}
// compute zero-based result and convert to 1-based
return josephusZeroBased(n, k) + 1;
}
/**
* Prints the survivor position (1-based) for given n and k.
*
* @param n number of people (must be > 0)
* @param k step count (must be > 0)
* @throws IllegalArgumentException if n <= 0 or k <= 0
*/
public static void printJosephus(int n, int k) {
int survivor = getJosephus(n, k);
System.out.println(survivor);
}
/**
* Recursive helper that returns the zero-based survivor index.
*
* @param n number of people
* @param k step
* @return zero-based survivor index
*/
private static int josephusZeroBased(int n, int k) {
// Base case
if (n == 1) {
return 0;
}
// Recurrence
return (josephusZeroBased(n - 1, k) + k) % n;
}
/**
* Demo method to show usage
*
* @param args command line arguments
*/
public static void main(String[] args) {
int n = 7;
int k = 3;
System.out.println("Josephus problem demo:");
System.out.printf("n = %d, k = %d%n", n, k);
int survivor = getJosephus(n, k);
System.out.println("Survivor (1-based position): " + survivor);
}
}