You are given an infinite sequence of positive integers: 1, 2, 3, 4, 5, 6, 7, 8, ...
An infinite number of operations are performed on this sequence: In the 1st operation, every 2nd element is removed. In the 2nd operation, every 3rd element of the remaining sequence is removed. In the 3rd operation, every 4th element is removed. In the i-th operation, every (i + 1)-th element is removed. These operations continue indefinitely.
You are given an integer x. Determine whether x will remain in the sequence forever or be removed at some operation.
Input : 13
1,2,3,4,5,6,7,8,9,10,11,12,13
1st operation (remove every 2nd):
1,3,5,7,9,11,13
2nd operation (remove every 3rd):
1,3,7,9,13
3rd operation (remove every 4th):
1,3,7,13
4th operation (remove every 5th): No 5th element exists
Answer: YES
Input : 17
Answer: NO








so in the first test we check if 13 is removed or not?
Nvm I was wrong
Can't find any pattern just brute force it man
You can use sieve of Eratosthenes with some minor adjustments and It may work!
https://oeis.org/A000960
Hard to tell how reasonable of an interview problem it is without the upper bound on x
Here is one pattern that I observed which might help out.
We know that after every operation that we do, the number x can either be present in the array or it will be removed from the array.
So when will x be removed from the array? When the index of the array where x is present currently is divisible by (i+1), where i is the operation number.
But what if that index is not divisible by (i+1)? Then the index where x is present will shift leftwards. Let's say x is at some index p currently. Then if p is not divisible by (i+1), p will now shift leftwards by p/(i+1) and will essentially become p -=(p/(i+1)
We can continue doing this until p >= (i+1). Why? Cus if (i+1) becomes greater than p, p cant be divisible by (i+1)
Get the initial position of the given num, and for every operation, keep checking whether it will be deleted and if not keep updating its position.
Question Link : https://codeforces.me/contest/2169/problem/D1
Track the number’s position in the sequence; in each round k (starting from 2), if its position is divisible by k it gets removed, otherwise its position shifts forward by subtracting how many numbers before it were removed (position / k),and this process continues until k becomes larger than its position, at which point the number is surely to survive forever. Time Complexity = O(√x).
Very similar to this problem.
The Interviewer was excepting the solution in O(1) time complexity