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.
Heard about Josephus algorithm:)
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?
can you send problem link,I want to test my sol before commenting.
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
Do you mean every operation is remove a card which is at top and then move the top card to the bottom, right?
For example if n is 4. So from top to bottom card number is 1,2,3,4.
First operation remove number 1 card and then move the number 2 card to bottom so it is 3,4,2.
Second operation remove number 3 card and then move the number 4 card to bottom so it is 2,4.
Third operation remove number 2 card and then move the number 4 card to bottom so it is 4.
It is only have one card, so the answer is 4, right?
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.
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..
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.
The rajkumar62506 code is more simple and efficiency.
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.
...
Hmm nice intuition .. question seems quite relatable to Josephus problem but the implementation is quite different