From fa52577c93f64ae2761e796381b491f29dc1fe4e Mon Sep 17 00:00:00 2001 From: Apurva Vats Date: Wed, 29 Oct 2025 00:45:14 +0530 Subject: [PATCH 1/3] Add Josephus Problem in Java (Fixes #3345>) --- archive/j/java/JosephusProblem.java | 32 +++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 archive/j/java/JosephusProblem.java diff --git a/archive/j/java/JosephusProblem.java b/archive/j/java/JosephusProblem.java new file mode 100644 index 000000000..64a23beb7 --- /dev/null +++ b/archive/j/java/JosephusProblem.java @@ -0,0 +1,32 @@ +import java.util.*; + +public class JosephusProblem { + + public static int josephus(int n, int k) { + int res = 0; + for (int i = 1; i <= n; i++) { + res = (res + k) % i; + } + return res + 1; + } + + public static void main(String[] args) { + if (args.length != 2) { + System.err.println("Usage: "); + System.exit(1); + } + + try { + int n = Integer.parseInt(args[0]); + int k = Integer.parseInt(args[1]); + if (n <= 0 || k <= 0) { + System.err.println("Invalid input"); + System.exit(1); + } + System.out.println(josephus(n, k)); + } catch (NumberFormatException e) { + System.err.println("Invalid input"); + System.exit(1); + } + } +} From 92d3568dfd59c5f3c3c99cb7a8ac01aaea50aac2 Mon Sep 17 00:00:00 2001 From: Apurva Vats Date: Wed, 29 Oct 2025 01:03:36 +0530 Subject: [PATCH 2/3] improved message --- archive/j/java/JosephusProblem.java | 33 ++++++++++++++++------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/archive/j/java/JosephusProblem.java b/archive/j/java/JosephusProblem.java index 64a23beb7..1361ca670 100644 --- a/archive/j/java/JosephusProblem.java +++ b/archive/j/java/JosephusProblem.java @@ -2,31 +2,34 @@ public class JosephusProblem { - public static int josephus(int n, int k) { - int res = 0; - for (int i = 1; i <= n; i++) { - res = (res + k) % i; - } - return res + 1; + private static int josephus(int n, int k) { + if (n == 1) return 1; + return (josephus(n - 1, k) + k - 1) % n + 1; } public static void main(String[] args) { + final String usage_msg = "Usage: please input the total number of people and number of people to skip.\n"; + if (args.length != 2) { - System.err.println("Usage: "); + System.err.print(usage_msg); System.exit(1); } + int n, k; try { - int n = Integer.parseInt(args[0]); - int k = Integer.parseInt(args[1]); - if (n <= 0 || k <= 0) { - System.err.println("Invalid input"); - System.exit(1); - } - System.out.println(josephus(n, k)); + n = Integer.parseInt(args[0]); + k = Integer.parseInt(args[1]); } catch (NumberFormatException e) { - System.err.println("Invalid input"); + System.err.print(usage_msg); + System.exit(1); + } + + if (n <= 0 || k <= 0) { + System.err.print(usage_msg); System.exit(1); } + + int result = josephus(n, k); + System.out.println(result); } } From 634cd9e124a1f18d8d2c3e1ebbf26e1a2f3250d6 Mon Sep 17 00:00:00 2001 From: Apurva Vats Date: Wed, 29 Oct 2025 01:10:34 +0530 Subject: [PATCH 3/3] Fix uninitialized variables in Josephus Problem Java --- archive/j/java/JosephusProblem.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/archive/j/java/JosephusProblem.java b/archive/j/java/JosephusProblem.java index 1361ca670..08d310536 100644 --- a/archive/j/java/JosephusProblem.java +++ b/archive/j/java/JosephusProblem.java @@ -15,7 +15,7 @@ public static void main(String[] args) { System.exit(1); } - int n, k; + int n=0, k=0; try { n = Integer.parseInt(args[0]); k = Integer.parseInt(args[1]);