Josephus problem Java

PROGRAM FOR JOSEPHUS PROBLEM



class Test 
{
  
    // Method for finding the winning child.
    private int josephus(int n, int k) 
    {
        int sum = 0;
  
        // For finding out the removed 
        // chairs in each iteration 
        for(int i = 2; i <= n; i++) 
        {
            sum = (sum + k) % i;
        }
  
        return sum+1;
    }
  
    // Driver Program to test above method 
    public static void main(String[] args)
    
        int n = 14
        int k = 2
        Test obj = new Test();
        System.out.println(obj.josephus(n, k)); 
    }
}


OUTPUT
13

Comments

Popular posts from this blog

Solve the Sudoku Python

Solve the Sudoku Java

Find Duplicates Java