Riyuzak251097's blog

By Riyuzak251097, history, 4 years ago, In English

I recently came across an interesting problem You are given n cards numbered from 1 to N with 1 at the top and N at the bottom. You are supposed to remove elements alternatively from the deck with removing the first card and then push the next card at the bottom of the deck. The process stops when one card is left. Find the Number of the last card left. Can someone help me in getting the intuition in solving the problem above. I was required to solve it in less than O(n) time complexity.

  • Vote: I like it
  • +7
  • Vote: I do not like it

»
4 years ago, # |
  Vote: I like it +3 Vote: I do not like it

Heard about Josephus algorithm:)

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Ya thought of the way using at but .. could not come to a solution of skipping the first person.. can u deduce some recurrence relation for this?

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      can you send problem link,I want to test my sol before commenting.

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Sorry the problem is not openly available.It is a part of the hackerearth c++ mock interview test of medium level. You gotta attempt the contest to submit it

»
4 years ago, # |
Rev. 2   Vote: I like it +10 Vote: I do not like it

Do you mean every operation is remove a card which is at top and then move the top card to the bottom, right?

Example

I think can use std::list(C++) or LinkedList(Java) to simulation operation. Because Them are based on a data structure--linked list. It can insert and delete element on $$$O(1)$$$.
So the total complexity is $$$O(n+\frac{n}{2}+\frac{n}{4}+\frac{n}{8}+...)$$$.
Because $$$1+\frac{1}{2}+\frac{1}{4}+\frac{1}{8}+...$$$ is small then 2.
So the complexity is small then $$$O(2n)$$$. Ignore the coefficient is $$$O(n)$$$.
We can also use mathematical methods. Here is oeis result.

  • »
    »
    4 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I am looking for something less than o(n) .. i cannot preprocess the queries. I get the idea of using a linked list but what if n is greater than 10^7..

    • »
      »
      »
      4 years ago, # ^ |
      Rev. 5   Vote: I like it +1 Vote: I do not like it

      I think follow code maybe work. Complexity is $$$O(log n)$$$.
      Looking for a law.
      1;//special
      2;//only 1 number
      2,4;//2 numbers
      2,4,6,8;//4 numbers
      2,4,6,8,10,12,14,16;//8 numbers
      2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32;//16 numbers
      It is a geometric progression.

      Code
»
4 years ago, # |
Rev. 2   Vote: I like it +6 Vote: I do not like it

Here is O(log(n)) sol,Idea is that after each cycle half of elements gets removed and also difference between remaining elements will gets double.Now keep track of first element which remains after each cycle,difference bw elements. Also as we are doing cyclic so if last element will be removed then in next iteration we will skip first element of remaining list and if last element remains then we need to remove first element of remaining list,this can done by keeping start variable which will be zero if first element is removed and will be 1 if 2nd element is removed and for next iteration we can count no of elements from start to last if they are odd that means last element will be removed and if they are even last element will survive and accordingly we can find first remaining and start for next iteration.

Code
  • »
    »
    4 years ago, # ^ |
      Vote: I like it +8 Vote: I do not like it

    Hmm nice intuition .. question seems quite relatable to Josephus problem but the implementation is quite different