4

A group of children form a ring. The first child is selected and they start counting clockwise from that child until a fixed number (n, which is given at the starting of the game) is reached. When the count reaches n, the child on the nth spot is eliminated. The game continues again starting with the next child and the process continues until a single child remains.Your aim is to print the position of the child which remains till last.

For example, if there are 10 children and the fixed number n is 6, the position of the last child who remains till the last is 3.

Is there a better programmatic algorithm for solving this problem?

P.S. I know that we can easily do this using arrays or other data structures. I just want the best strategy, preferably a mathematical approach.

Vishnu Vivek
  • 1,819
  • 1
  • 20
  • 30
  • 4
    I'm sorry but didn't they start with a random starting position? How is it possible to give any certain answer in your example without the starting position? – Alan Feb 20 '13 at 19:47
  • Or just use modulo arithmetic. –  Feb 20 '13 at 19:48
  • because you can name them 1-10. the randomness refers to who starts. his example doesn't include the starting position, but you can imagine that there is a definite answer. – 75inchpianist Feb 20 '13 at 19:49
  • @H2CO3 That occurred to me, but there is a bit more to it, since starting position is unknown and you resume after the next child and may loop multiple times.. If he could answer me question the answer seems pretty simple though with a loop and modulus – Alan Feb 20 '13 at 19:49
  • @Alan They start with the first child. I'll edit it. Thanks!! – Vishnu Vivek Feb 20 '13 at 19:50
  • If you know the maximum number of children, you could precompute a table and then hard-code it. – John Dvorak Feb 20 '13 at 19:53
  • 12
    It's the Josephus problem, just without killing. – Daniel Fischer Feb 20 '13 at 19:53
  • 4
    http://en.wikipedia.org/wiki/Josephus_problem – John Dvorak Feb 20 '13 at 19:54
  • @DanielFischer wow! that's a surprise.. Thanks! – Vishnu Vivek Feb 20 '13 at 19:56
  • 1
    @LittleBobbyTables: Not everyone in the group was as devoted to the cause of "death before capture" as the people who thought up the cunning plan. – Mason Wheeler Feb 20 '13 at 20:04
  • 1
    @DanielFischer "the child on the nth spot is eliminated ... until a single child remains." -- are you sure about the "without killing" part? – John Dvorak Feb 20 '13 at 20:07
  • @JanDvorak Now you mention it, with today's children, I'm not. Back in my day, we had limits, though. – Daniel Fischer Feb 20 '13 at 20:13

1 Answers1

2

I think the easiest way is still write a recurrence (pretty much what wikipedia says, give upvotes to Jan Dvorak):

T(1) = 0
T(c) = (T(c-1)+n) mod c

Or, writing as C (with no recursion):

int non_fatal_josephus(int children, int n) {
    int result = 0;
    for(int i=2; i<=children; i++)
        result = (result + n) % i;

    return result + 1; //to make it one-based
}

Recurrence explanation:

T(c) means "which child will win if we start with c children".

T(1) = 0 because if we have only 1 children, she already won (the 1st child, index 0).

The general case is that we always eliminate the nth children (index n-1), as soon we eliminate her, we start counting again with (c-1) children, but this time, instead of starting at index 0, we start at index n. That explains the +n: T(c-1) assumes the counting starts at 0. We use the +n to shift the child index as if we started at index n.

Juan Lopes
  • 10,143
  • 2
  • 25
  • 44
  • 1
    @Juan Lopes - thanks, I am trying to work out manually the indexes of children eliminated in each round and they don't seem to match up with what the recurrence relation returns, but the final answer does match up. Can you help explain more about what the recurrence relation T(c) is and what c is? – goldenmean Feb 22 '13 at 12:19
  • @goldenmean Added a recurrence explanation to the answer. – Juan Lopes Feb 22 '13 at 14:52