Josephus Problem

Hard Recursion PRO
🔒 Login to Unlock

Problem Statement

Write a C program using recursion to solve the Josephus problem (surviving position for n people and step k).

Input Format

Two integers: n (people) and k (step count).

Output Format

Print the 0-indexed or 1-indexed safe position.

Constraints

1 <= n <= 50, 1 <= k <= 10

Sample Input

5 2

Sample Output

3

Explanation

Recursive relation: J(n, k) = (J(n - 1, k) + k) % n with base case J(1, k) = 0.

Starter Code

#include <stdio.h>

int josephus(int n, int k) {
    // Write your recursive logic here
}

int main() {
    int n, k;
    if (scanf("%d %d", &n, &k) == 2) {
        printf("%dn", josephus(n, k) + 1); // 1-indexed output
    }
    return 0;
}

Limits

  • Time Limit: 1s
  • Memory Limit: 256MB

Embedded C Programming

Updated: March 15, 2026
Intermediate

Embedded systems rely on efficient low-level programming to interact directly with hardware. In this course, you will learn how to write practical Embedded C programs used in real microcontroller-based systems. Rather than focusing only on theory, this course follows a practice-driven approach. Each lesson includes hands-on coding exercises that simulate real firmware development tasks used