Some people are standing in a queue. A selection process follows a rule where people standing on even positions are selected. Of the selected people a queue is formed and again out of these only people on even position are selected. This continues until we are left with one person. Find out the position of that person in the original queue.
How can I modify the code so that it works when queue has odd no of elements in it because at some point(in the while loop),queue will certainly have odd no of elements.
int main()
{
int temp=0,x,n,i;
cin>>x;
while(x--) {
cin>>n;
queue<int> q;
for(i=1;i<=n;i++)
q.push(i);
while(q.size()!=1) {
q.pop();
temp=q.front();
q.pop();
q.push(temp);
}
cout<<temp;
}
return 0;
}
input:5 Expected ouput:4