The Problem
Imagine you have a circle of people and you go around the circle removing every second person until one person is left.
If you have 3 people in the circle, then the 3rd person will be the last one remaining. If you have 4 people then the 1st person will be the last one remaining. If you have 11 people then the 7th person will be the one remaining.
If you have N people in the circle, who will be the last one remaining?
Please formulate your answer as a general solution and not as an algorithm that simulates the problem
Solution
Let N be the number of people in the circle and bi...b1b0 its binary representation. The position (in binary representation) fk...f1f0 of the last person remaining in the circle is expressed by the formula below:
......+----------------------------------------------------------------------------------------------
.......| 1, where k = 0
.......|
fk = | if fk-1 = bk-1 then 1 , where k > 0 and k ≤ i and fk...f1f0 ≤ bi...b1b0
.......| else 0
......+----------------------------------------------------------------------------------------------
Examples:
N = 3 => b1b0 = 11
f0 = 1
f1 = f0 (1) = b0 (1) => 1
Result: f1f0 = 11 => 3
N = 4 => b2b1b0 = 100
f0 = 1
f1 = f0 (1) ≠ b0 (0) => 0
f2 = f1 (0) = b1 (0) => 0, but f2f1f0 > b2b1b0 => 101 > 100 then it is discarded
Result: f1f0 = 01 => 1
N = 11 => b3b2b1b0 = 1011
f0 = 1
f1 = f0 (1) = b0 (1) => 1
f2 = f1 (1) = b1 (1) => 1
f3 = f2 (1) ≠ b2 (0) => 0
Result: f3f2f1f0 = 0111 => 7
Built With
- text-description
Log in or sign up for Devpost to join the conversation.