How to solve the Josephus Problem?

A Puzzling Survival Game

Do you like puzzles and mathematical problems?  Then, get ready to play the Josephus game! It's a game of survival where the winner is the last person standing.

Consider N people standing in a circle. Every K-th person is eliminated until only one person remains. The task is to find the last person.

Problem Statement

We use a recursive function that calculates the position of the survivor in a circle of N people, with every K-th person being eliminated.

Approach 1-  Recursive Approach

Algorithm -  1. Step 1: If N == 1, return 1.  2. Step 2: Else, return (josephus(N – 1 , K) + K – 1) % N + 1.   Curious to see it in action? 

Simplify the Josephus problem with a list approach! Initialize a list of integers from 1 to N, then repeatedly remove the K-th element until only one remains.

Approach 2-  List Method

If N is even, all even positions get deleted leaving N/2. The solution?  josephus(2 * N, 2) = josephus(N, 2) - 1   And for N odd? Find out by clicking here!

Special Case:  K=2  A Game Changer!

Click on the link below to start your journey.

Are you ready to up your coding game?

Step Up Your Game with InterviewBit Web Stories

Don't miss out on the chance to upskill yourself with IntervewBit's engaging web stories.