Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

we are given 1....n numbers. lets say we are to save a given number element k for last in elimination. We start eliminating them in the following manner. I eliminate 1 at first. Then eliminate the next kth element (index k+1). and then keep doing the same wrapping around n if needed. The numbers eliminated before dont count for elimination for counting till k, next time onwards. I want to find such k that element given element no (say i) goes last in the elimination.

as sample : lets take 1 2 3 4 5 as input

now say i want to save 5 for last. Thus min number k=3 in this case (ans)

Lets simulate it.
1 is eliminated first
4 goes next
3 goes next
2 goes next
and 5 is saved for last.

I need to find this minimum (k=3) given n and number i to save for last.

share|improve this question
This is the Josephus problem. –  joriki Feb 13 '13 at 11:21

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.