Consider a game where there are ** N **children (numbered 1,2 … N) in a circle. During the game,

**from the circle until there are no children left. In which order will the children be removed?**

**every other child is removed****Examples:**

N = 7Input:2 4 6 1 5 3 7Output:Explanation:

- Children = {1, 2, 3, 4, 5, 6, 7}, 1 is skipped and 2 is removed.
- Children = {1, 3, 4, 5, 6, 7}, 3 is skipped and 4 is removed.
- Children = {1, 3, 5, 6, 7}, 5 is skipped and 6 is removed.
- Children = {1, 3, 5, 7}, 7 is skipped and 1 is removed.
- Children = {3, 5, 7}, 3 is skipped and 5 is removed.
- Children = {3, 7}, 7 is skipped and 3 is removed.
- Children = {7}, 7 is skipped and removed.

N = 6Input:2 4 6 3 1 5Output:Explanation:

- Children = {1, 2, 3, 4, 5, 6}, 1 is skipped and 2 is removed.
- Children = {1, 3, 4, 5, 6}, 3 is skipped and 4 is removed.
- Children = {1, 3, 5, 6}, 5 is skipped and 6 is removed.
- Children = {1, 3, 5}, 1 is skipped and 3 is removed.
- Children = {1, 5}, 5 is skipped and 1 is removed.
- Children = {5}, 5 is skipped and removed.

** Approach: **To solve the problem, follow the below idea:

The problem can be solved using a Queue to simulate the removal of elements. Initially, we will push all the elements into the queue in order 1 to N. Now, we can take a flag which we can toggle after every element as we want to remove only the alternate elements. So, if the flag is true, we remove the current element and toggle flag to false so that the next element does not get removed. Now, for the next element the flag is false, so we don’t remove that element and set the flag to true so that the next element gets removed. After popping the element from the queue, if flag = true we will print the element otherwise we will push the element back to the queue.

**Step-by-step algorithm:**

- Declare a Queue
and push all the children into the queue.**q** - Also declare a
to decide whether to remove the current child or not (Initially set to false).**flag** - Now, one by one pop the element at the front of the queue.
- If the element is supposed to be removed from the queue, we print the element.
- Otherwise, we push the element back to the queue.

- All the elements will be printed in the order as they are deleted.

Below is the implementation of the algorithm:

#include <bits/stdc++.h>
#define ll long long int
using namespace std;
void solve(ll N)
{
queue<ll> q;
// Push all the children to the queue
for (int i = 1; i <= N; i++)
q.push(i);
// Set the flag to false, so that the first child does
// not get removed
bool flag = false;
while (!q.empty()) {
int ele = q.front();
q.pop();
// If we have to remove the element, print it
if (flag) {
cout << ele << " ";
}
// If we don't have to remove the element, push it
// back to the queue
else {
q.push(ele);
}
// Toggle the value of flag so that only the
// alternate elements get removed
flag = !flag;
}
}
int main()
{
ll N = 7;
solve(N);
}

import java.util.LinkedList;
import java.util.Queue;
public class Main {
static void solve(long N) {
Queue<Long> q = new LinkedList<>();
// Push all the children to the queue
for (long i = 1; i <= N; i++)
q.add(i);
// Set the flag to false, so that the first child does
// not get removed
boolean flag = false;
while (!q.isEmpty()) {
long ele = q.poll();
// If we have to remove the element, print it
if (flag) {
System.out.print(ele + " ");
}
// If we don't have to remove the element, push it
// back to the queue
else {
q.add(ele);
}
// Toggle the value of flag so that only the
// alternate elements get removed
flag = !flag;
}
}
public static void main(String[] args) {
long N = 7;
solve(N);
}
}
// This code is contributed by shivamgupta0987654321

from collections import deque
def solve(N):
q = deque(range(1, N+1))
flag = False
while q:
ele = q.popleft()
if flag:
print ele,
else:
q.append(ele)
flag = not flag
N = 7
solve(N)

using System;
using System.Collections.Generic;
class Program
{
static void Solve(long N)
{
Queue<long> q = new Queue<long>();
// Push all the children to the queue
for (long i = 1; i <= N; i++)
q.Enqueue(i);
// Set the flag to false, so that the first child does
// not get removed
bool flag = false;
while (q.Count > 0)
{
long ele = q.Dequeue();
// If we have to remove the element, print it
if (flag)
{
Console.Write(ele + " ");
}
// If we don't have to remove the element, push it
// back to the queue
else
{
q.Enqueue(ele);
}
// Toggle the value of flag so that only the
// alternate elements get removed
flag = !flag;
}
}
static void Main(string[] args)
{
long N = 7;
Solve(N);
}
}

function solve(N) {
let q = [];
for (let i = 1; i <= N; i++) {
q.push(i);
}
let flag = false;
while (q.length > 0) {
let ele = q.shift();
if (flag) {
console.log(ele);
} else {
q.push(ele);
}
flag = !flag;
}
}
const N = 7;
solve(N);

**Output**

2 4 6 1 5 3 7

** Time Complexity: **O(2 * N) = O(N), where

**is the number of children.**

**N****O(N)**

**Auxiliary Space:**