Snippets Collections
// Time Complexity : O(n)

import java.util.*;
import java.io.*;

class GFG 
{ 
    static int check(int n, int k)
    {
    	if(n == 1)
    		return 0;
    	else
    		return (check(n - 1, k) + k) % n;
    }

    static int josephus(int n, int k)
    {
    	return check(n, k) + 1;
    }
      
    public static void main(String args[]) 
    { 
        System.out.println(josephus(5, 3));  
    } 
}
star

Sun Feb 06 2022 22:06:53 GMT+0000 (Coordinated Universal Time) https://practice.geeksforgeeks.org/problems/josephus-problem/1/?track=DSASP-Recursion&batchId=190

#java #gfg #geeksforgeeks #lecture #recursion #josephusproblem

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension