Josephus problem | Practice | GeeksforGeeks

Given the total number of persons n and a number k which indicates that k-1 persons are skipped and kth person is killed in circle in a fixed direction.
The task is to choose the safe place in the circle so that when you perform these operations starting from 1st place in the circle, you are the last one remaining and survive. 
Example 1:
Input:
n = 3 k = 2
Output: 3
Explanation: There are 3 persons so 
skipping 1 person i.e 1st person 2nd 
person will be killed. Thus the safe 
position is 3.
 
Example 2:
Input:
n = 5 k = 3
Output: 4
Explanation: There are 5 persons so 
skipping 2 person i.e 3rd person will 
be killed. Thus the safe position is 4.

 
Your Task:
You don't need to read input or print anything. You are required to complete the function josephus () that takes two parameters n and k and returns an integer denoting safe position. 
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(N).
Constraints:
1 <= k, n <= 20
0
mitradiptamoy1 week ago
//ITERATIVE SOLUTION IN C++ 
 
int josephus(int n, int k)   {       int res=0;      for(int i=1;i<=n;i++){         res=(res+k)%i;      }      return res+1;   }
0
dipanshusharma93131 week ago
// java solution
class Solution{   public int jose(int n, int k){       if(n == 1){           return 0;       }       return (jose(n-1, k) + k) % n;   }  public int josephus(int n, int k)   {       //Your code here      return jose(n,k) + 1;   }
}
+2
deekshakulshreshtha113 weeks ago

class Solution
{
    public:
    int josephus(int n, int k)
    {
        
        return (accurate(n, k))+1; 
        
    }
    
    int accurate(int n, int k){
        if(n==1)
            return 0; 
            
        return ((accurate(n-1, k)+k)%n);
    }
};

0
subhajitghosh19973 weeks ago
// java code  // all test cases pass
public int josephus(int n, int k)   {       //Your code here       return check(n,k) + 1;   }      public int check(int n , int k){              if(n == 1){           return 0;       }       return (check(n-1,k)+k)%n;   } 
+4
kashyapjhon1 month ago
C++ Solution Time=(0.0/1.3) EASY:
int help(vector<int> arr, int index, int k,int &ans){   if(arr.size()==1){       ans=arr[0];       return ans;   }      index=(index+(k-1))%arr.size();      arr.erase(arr.begin()+index);      help(arr,index,k,ans);   }
  int josephus(int n, int k)  {  //Your code here     int ans=-1;     int index=0;     vector<int> arr;     for(int i=1;i<=n;i++){         arr.push_back(i);     }     help(arr,index,k,ans);     return ans;  }
+3
vermaashitiit1 month ago
//Iterative Approach      vector<int> v;      int i;      for(i=0;i<n;i++){          v.push_back(i+1);      }      i=(k-1)%n;      while(v.size()!=1){         v.erase(v.begin()+i);         n--;         i=(i+k-1)%n;      }      return v[0];
+2
ayushmishra29Premium1 month ago
public int josephus(int n, int k)   {       if(n == 1) {           return n;       }       return (josephus(n - 1, k) + (k - 1)) % n + 1;   }
0
vineettiwari0419971 month ago
class Solution{   public:   int jos(int n,int k)   {       if(n==1)       return 0;       return (jos(n-1,k)+k)%n;   }   int josephus(int n, int k)   {        return jos(n,k)+1;      //Your code here   }};
0
coderh1 month ago
class Solution:
    def josephus(self,n,k):
        person=[]
        for i in range(1,n+1):
            person.append(i)
        self.helper(person,k-1,0)
        return person[0]
        
    def helper(self,person,k,index):
        if len(person) == 1:
           return
   
  # find the index of first person which will die
        index = ((index+k)%len(person))
   
   # remove the first person which is going to be killed
        person.pop(index)
   
  # recursive call for n-1 persons
        self.helper(person,k,index)
0
coltron6662 months ago
All test cases passed python:
 
class Solution:   def josephus(self,n,k):       #Your code here       if n==1:           return 1       t = (self.josephus(n-1,k) + k-1)%n       return t+1
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.