Index of the remaining student
Problem Statement
Design an object-oriented system that determines the index (position) of the last remaining student after repeatedly eliminating every k-th student from a circular arrangement of n students.
This is a variation of the Josephus Problem, focusing on returning the index (not the student ID or name).
🎯 Objective
Given:
After repeatedly removing every K-th student until only one remains, determine the index of the last remaining student.
Your design should emphasize modularity, reusability, and extensibility, allowing variations like different counting directions or skip patterns.
✅ Functional Requirements
⚙️ Non-Functional Requirements
Aspect with Description
Time Complexity
O(N) or better (use optimized recurrence if possible).
Scalability
Should handle up to millions of students.
Extensibility
Easy to plug in different elimination rules or data structures.
Testability
Each logical component must be unit-testable.
Maintainability
Use clean OOP design principles.
🧱 Key Classes
1. StudentCircle
Represents the circular group of students. Handles traversal and elimination logic.
1
2class StudentCircle {
3 private List<Integer> students;
4 public int removeKthStudent(int k);
5 public boolean hasMoreThanOne();
6}2. JosephusSolver
Encapsulates the logic to find the index of the last remaining student using simulation or mathematical recursion.
1
2class JosephusSolver {
3 public int findRemainingIndex(int n, int k);
4}3. JosephusApp
Driver class to take user inputs and display results.
1
2class JosephusApp {
3 public static void main(String[] args);
4}4. (Optional) EliminationStrategy
Interface to allow plugging different solving strategies (simulation vs mathematical formula).
1
2interface EliminationStrategy {
3 int getRemainingIndex(int n, int k);
4}5. (Optional) Implementations
1
2f(1) = 0
3f(n) = (f(n - 1) + k) % n🔄 Example Flow
Input:
1
2N = 7, K = 3Process:
Elimination order → [2, 5, 1, 6, 4, 0]
Remaining student → index 3
Output:
1
2The index of the remaining student is: 3💾 Possible Extensions
🧠 Key Design Considerations
Use modular strategy pattern:
✅ Expected Output Examples
Input and Output with Explanation
N = 1, K = 1
0
Only one student remains
N = 5, K = 2
2
Remaining index = 2
N = 7, K = 3
3
Remaining index = 3
N = 10, K = 1
9
Always the last index when K = 1
📘 Formula Reference (Mathematical Approach)
Recursive definition for Josephus position:
1
2f(1) = 0
3f(n) = (f(n - 1) + k) % nFinal survivor (0-based index) = f(N)