Algorithm of 10940 - Throwing cards away II

Problem Description Link
Algorithm:

This problem same as 10935 .
but if you use queue to solve this problem may be you get time limit exit so you can use any tricky method .

You can follow this algorithm:

   1. Take the input n.
   2.  Let i is an integer initialized by 1.
   3. Loop until i>=n
    i = i*2
   4. mod = i%n
   5. Result = n – mod
   6. print the result.
NB:1. By multiplying i with 2, we are actually tracking the cards after throwing and moving at the bottom.
2. By mod = i%n and Result = n – mod,  we are getting the card position and card number.
image

No comments:

Post a Comment

Write your comment - Share Knowledge and Experience