Explanation for recursive implementation of Josephus problem
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
The Josephus problem is a theoretical problem related to a certain elimination game, based on an ancient story about Josephus, a Jewish historian. This problem can be solved using various methods, and one of the most intuitive solutions is through recursive implementation. In this article, we'll delve into a recursive approach to solving the Josephus problem, detailing the technical aspects and providing examples to enhance understanding.
The Problem Statement
Consider `n` people standing in a circle, numbered from 1 to `n`. Starting from a specific position, every `k-th` person is eliminated from the circle. The process is repeated until only one person remains. The task is to determine the position of the last person remaining.
Recursive Solution Explanation
Base Case
The base case of the recursion defines when the problem is trivially solvable. For the Josephus problem, the base case occurs when there is only one person (`n = 1`). In this situation, no person is eliminated, and the survivor is obviously the one person left:
This means that the position of the survivor is `0` in 0-based indexing.
Recursive Case
To solve the problem recursively for `n` people, we need to relate it to the solution of the problem with `n-1` people. The core idea is that eliminating every `k-th` person from a group of `n` people reduces the problem to eliminating every `k-th` person from `n-1` people. The trick is to adjust the position due to the circular nature of the circle:
Here's how the recursive logic works:
- Calculate the Josephus position for `n-1` people.
- Shift this position because the circle has shifted by moving `k` positions.
- Apply modulo `n` to wrap around the circle if necessary.
Complete Recursive Function
A simple recursive function in Python looks like this:
- Different counting directions.
- Varying steps `k` depending on certain conditions or person attributes.
- Network topology design to ensure fail-safe connectivity.
- Memory and resource management in operating systems through circular buffering techniques.
- Cryptographic algorithms that use similar elimination tactics for key generation or verification.
Related reading
- Explanation of Algorithm for finding articulation points or cut vertices of a graph
- Explanation of Merge Sort for Dummies
- Exposé Layout Algorithm
- Extending Python's os.walk function on FTP server
- Extracting 2 numbers n times and placing back the addition in On instead of Onlogn
- Extreme optimization of integer binary search
- Extremely fast method for modular exponentiation with modulus and exponent of several million digits
- Face clustering using Chinese Whispers algorithm

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.