tried a solution with Array variable even it worked with small numbers, it would cause outofmemory errors when the input is a large integer. I had to find a better solution. Then I came up with an idea of recording the number on the very left and the distance from one number to the next one, as each round the logic would remove half numbers, it would be easy to figure how many rounds are needed to get only 1 number left. Here's the code of the solution which is accepted.

```
public int LastRemaining(int n)
{
if (n == 1)
return 1;
//0:
int distance = 1;
int left = 1;
while (true)
{
if (n > 1)
{
//1
left += distance;
distance += distance;
//2
n = n / 2;
if (n > 1)
{
if (n % 2 == 1)
{
left += distance;
}
n = n / 2;
distance += distance;
}
}
else
break;
}
return left;
}
```