Solving Josephus Problem in Java7 May 2025 | 7 min read The Josephus Problem is a theoretical problem related to a certain elimination game. It is called after the Jewish historian Flavius Josephus, who, as per legend, created this technique to evade capture during a siege. Problem Statement
Example 1: Input: n = 6, k = 4 Output: 5 Explanation: The persons at positions 4, 1, 5, 2, and 6 are killed in order, and the person at position 3 survives. Example 2: Input: n = 10, k = 3 Output: 4 Explanation: The persons at positions 3, 6, 9, 2, 5, 8, 1, 7, and 10 are killed in order, and the person at position 4 survives. Approach: Using ListThe Josephus problem can be solved using a list to simulate the elimination process. People are stored in a list, and in each iteration, the person at the calculated index is removed until only one person remains. AlgorithmStep 1: Initialize a list of people from 1 to n, where each element represents a person in the circle. Step 2: Set the starting index to 0, which represents the first person in the circle. Step 3: Iterate while the list size is greater than 1:
Step 4: Update the list size after each elimination and continue the process until only one person remains. Step 5: Return the last remaining person from the list, which is the safe position. ImplementationFile Name: JosephusProblemList.java Output: The safe position is: 3 Time Complexity: O(n²) Auxiliary Space Complexity: O(n) Using Iterative ApproachThe iterative approach to solving the Josephus problem calculates the safe position by iteratively updating the position for each number of people in the circle. It avoids recursion and directly computes the result with a single loop, achieving linear time complexity and constant space usage. Algorithm
ImplementationFile Name: JosephusProblemIterative.java Output: The safe position is: 3 Time Complexity: O(n) The loop runs from 2 to n, performing constant time operations inside. Auxiliary Space Complexity: O(1) The algorithm uses only a few integer variables (safePosition, i), requiring constant space. Josephus Problem in Linear Time and Constant SpaceThe Josephus Problem in linear time and constant space calculates the safe position iteratively, updating the position with each step using the formula (safePosition + k) % i. This approach eliminates the need for recursion, achieving O(n) time complexity and O(1) auxiliary space. AlgorithmStep 1: Initialize the safe position to 0, assuming only one person remains (0-based index). Step 2: Iterate from 2 to n (total number of people in the circle):
where i is the current size of the circle. Step 3: Update the safe position after each iteration to account for the eliminated person. Step 4: Continue until the loop reaches n to find the safe position for all n people. Step 5: Convert the safe position to a 1-based index by adding 1 and return it as the final result. ImplementationFile Name: JosephusProblemConstantSpace.java Output: The safe position is: 3 Time Complexity: O(n) Auxiliary Space Complexity: O(1) Josephus Problem Using RecursionThe Josephus Problem using recursion solves the problem by reducing the number of people in the circle one by one, calculating the safe position for n-1 people and adjusting the result based on the elimination step k. The recursive formula is josephus(n - 1, k) + k) % n, which finds the new safe position after each elimination. AlgorithmStep 1: If only one person (n = 1), return 0 (the safe position is the first person). Step 2: Call the function josephus(n - 1, k) to find the safe position for n - 1 people. Step 3: Use the formula (josephus(n - 1, k) + k) % n to adjust the position after one person is eliminated. Step 4: Return the safe position for n people. Step 5: Convert the result from 0-based to 1-based index by adding 1 and print the safe position. ImplementationFile Name: JosephusProblemRecursion.java Output: The safe position is: 3 Time Complexity: O(n) Auxiliary Space Complexity: O(n) Next TopicSum of Prime Numbers in Java |
DecimalFormat getMinimumIntegerDigits() method in Java
There is a built-in function in java.text called getMinimumIntegerDigits().The Java class DecimalFomrat is used to determine the smallest number of digits that can be within an integral component of a number. The portion that appears before the decimal point (.) in a number is known as...
2 min read
DTO Java
Enterprise application architecture patterns play a vital role in dealing with a lot of complex data. These are the standardized solution for large system's common problems. Enterprise application allows us to manipulate, display and store massive amounts of data. When we work on enterprise applications, we...
5 min read
for loop enum Java
In a computer language, enumerations are used to express a set of named constants. For instance, the four spades in a deck of starting to play cards could be represented by the enumerators Club, Diamonds, Heart, and Spade, which are members of the enumerated type...
4 min read
Difference Between Jdeps and Jdeprscan tools in Java
Difference Between Jdeps and Jdeprscan Tools in Java When it comes to developing and maintaining Java applications, tools that aid in dependency analysis and identifying deprecated APIs are invaluable. Two such tools provided by the Java platform are Jdeps and Jdeprscan. Despite their seemingly similar purposes, these...
3 min read
Open and Closed Hashing in Java
In this article, we are going to learn about Open Hashing and Closed Hashing in the Java programming language. By the end of the article, we will cover different parts of the topic, such as why these techniques are used in the Java programming language, what...
22 min read
Count Paths in Given Matrix in Java
A typical problem in computer science is counting pathways in a given matrix that can be solved in a number of ways. In this section, we will discuss three different ways to count path in the given matrix in Java. Problem Statement We have given a 2D...
7 min read
Mirror of Matrix Across Diagonal in Java
The mirror of a matrix across its diagonal involves flipping its rows and columns to reflect elements symmetrically. For a square matrix, the element at position (i, j) swaps with (j, i). The operation transforms the matrix into its transpose, useful in various mathematical and computational...
9 min read
How to Sort an Array in Java
The sorting is a way to arrange elements of a list or array in a certain order. The order may be in ascending or descending order. The numerical and lexicographical (alphabetical) order is a widely used order. In this section, we will learn how to sort array...
6 min read
Java Fibers
In this article, we are going to find out what are and when and where they are used in the Java programming language. What is ? The are also known as Java Virtual Machine (JVM) Fibers in the programming context. The JVM Fibers are user-mode threads...
3 min read
Concurrent Collections in Java
Java offers a number of data systems that allow the developers to work with collections of records effectively. When more than one threads are involved, concurrent collections come to be essential to make sure data integrity and thread safety. In this section, we will discover concurrent...
5 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India