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:
- 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.
Input: N = 6
Output: 2 4 6 3 1 5
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 q and push all the children into the queue.
- Also declare a flag to decide whether to remove the current child or not (Initially set to false).
- 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:
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)
Similar Reads
CSES Solutions - Josephus Problem II Consider a game where there are N children (numbered 1,2 ... N) in a circle. During the game, repeatedly K children are skipped and one child is removed from the circle. In which order will the children be removed? Examples: Input: N = 7, K = 2Output: 3 6 2 7 5 1 4Explanation: children = [1, 2, 3, 4
11 min read
Class 11 RD Sharma Solutions - Chapter 1 Sets - Exercise 1.2 Question 1. Describe the following sets in Roster form: (i) {x : x is a letter before e in the English alphabet} (ii) {x â N: x2 < 25} (iii) {x â N: x is a prime number, 10 < x < 20} (iv) {x â N: x = 2n, n â N} (v) {x â R: x > x} (vi) {x : x is a prime number which is a divisor of 60} (v
9 min read
Class 10 RD Sharma Solutions- Chapter 1 Real Numbers - Exercise 1.2 | Set 1 In this article, we delve into the solutions for Exercise 1.2 Set 1 from Chapter 1 of RD Sharma's Class 10 Mathematics textbook focusing on "Real Numbers". This chapter is fundamental as it introduces the concept of the real numbers which form the basis for understanding more complex mathematical id
15+ min read
NCERT Solutions for Class 10 Maths Chapter 11 Constructions NCERT Solutions Class 10 Maths Chapter 11 Constructions resource was created by GFG Team to help students with any queries they might have as they go through problems from the NCERT textbook. It lessens the frustration of spending a long time working on a problem. The NCERT Solutions for Class 10 Ma
15+ min read
Class 9 NCERT Solutions- Chapter 1 Number System - Exercise 1.5 Question 1: Classify the following numbers as rational or irrational: (i) 2 ââ5 (ii) (3 +â23)- â23 (iii) 2â7 / 7â7 (iv) 1/â2 (v) 2Ï Solution: (i) 2 ââ5 As â5 = 2.2360678⦠which is non-terminating and non-recurring. It is an irrational number. When we substitute the value of â5 in equation 2 ââ5, we
4 min read
Class 9 NCERT Solutions - Chapter 10 Circles - Exercise 10.3 Question 1. Draw different pairs of circles. How many points does each pair have in common? What is the maximum number of common points? Solution: (i) Two points common (ii) One point common (iii) One point common (iv) No point common (v) No point common As we can analyse from above, two circles can
3 min read