Interesting Interview Question

Josephus Problem

The problem has following recursive structure.

josephus(n, k) = (josephus(n - 1, k) + k-1) % n + 1
josephus(1, k) = 1

// Java code for Josephus Problem
import java.io.*;

class Josephus {

static int josephus(int n, int k)
{
    if (n == 1)
        return 1;
    else
        return (josephus(n - 1, k) + k - 1) % n + 1;
}
public static void main(String[] args)
{
    int n = 14;
    int k = 2;
    System.out.println("The chosen place is "
                       + josephus(n, k));
}

}

Comments (0)