Hi, lately I was solving Josephus Problem I and Josephus Problem II but getting TLE.

In my approach, I first take all the values from 1 to n in a vector. Then,print (pos+k)th value from vector and then erase the pos .Here, the **erase** function creates extra time complexity as vector erase costs O(n) times.

Now,please help me with that. Thank you.

## My code-(I)

```
signed main()
{
int n,k;cin>>n;
vector<int>josep;
for(int i=1;i<=n;++i)josep.push_back(i);
int pos=0;
while((int)josep.size()>1)
{
pos=(pos+1)%(int)josep.size();
cout<<josep[pos]<<' ';
josep.erase(josep.begin()+pos);
pos%=(int)josep.size();
}
cout<<josep.at(0)<<' ';
cout<<endl;
return 0;
}
```

## My code-(II)

```
signed main()
{
int n,k;cin>>n>>k;
vector<int>josep;
for(int i=1;i<=n;++i)josep.push_back(i);
int pos=0;
while((int)josep.size()>1)
{
pos=(pos+k)%(int)josep.size();
cout<<josep[pos]<<' ';
auto it=find(josep.begin(),josep.end(),josep[pos]);
josep.erase(it);
pos%=(int)josep.size();
}
cout<<josep.at(0)<<' ';
cout<<endl;
return 0;
}
```