Back to Problem Solutions forum
With that code the sample n = 10 and k = 3 is giving me 4. I have checked with the debugger every step and it seems that the numbers are disappearing correctly. However, when I paste the code for checking,I receive wrong answer.
What I am doing wrong? Could anyone help me?
Hm... I guess there are not too many people using C#
... :(
Have you tested the code with other input pairs, like 4 2
? It seems the result is not quite correct, if I am not mistaken myself...
With 4 2 it gives me 3. It should be 1, tough. So, my logic is wrong. On Wikipedia they are solving it very easily with recursion, but it is very hard for me to understand what is going on with the values. I will try to think of something else.
I'm not sure I can easily understand the formula from Wikipedia, though I once read its proof somewhere but now I forgot of it again.
However since values are not very large the exercise could be solved with direct counting.
I used the Wikipedia algorithm (please don’t tell my family).
I used a Queue for this problem. By using Enqueue(Dequeue) in an if statement with a counter I managed to get the right results. I must say though that I don't know if it is the best approach as I am still new to programming.