A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/competitive-programming/cses-solutions-josephus-problem-i/ below:

CSES Solutions - Josephus Problem I

CSES Solutions - Josephus Problem I

Last Updated : 23 Jul, 2025

Consider a game where there are N children (numbered 1,2 ... N) in a circle. During the game, every other child is removed from the circle until there are no children left. In which order will the children be removed?

Examples:

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

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

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:

Below is the implementation of the algorithm:

C++
#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);
}
Java
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
Python
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)
C#
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);
    }
}
JavaScript
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);

Time Complexity: O(2 * N) = O(N), where N is the number of children.
Auxiliary Space: O(N)



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4