Problem Solving‎ > ‎

Door Flipping

You have a number written starting from 1 to infinitive. You are performing some iteration. In 
1st iteration you are removing number at a gap of one , in second iteration you are removing 
number at a gap of two in remaining list and so on.You are given a number you have to tell that 
it will be remove after certain iteration or not.Also calculate the complexity of you algorithm and 
improve it if you can. 


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Let element to check is e

for (run = 2; run < input_runs; run++)
{
    if(0 ==  (e - 1) % run)
        Element Gone;
    else
        (e - 1) - (e - 1)/run;
}

(e - 1) is done as 1st element is always gone, so 2nd, 3rd, 4th element has to go next.


Comments