`{"id":"823c07f4-f71b-45f7-a9f3-29c2a2ee5fb2","name":"Josephus Problem","description":"1. You are given two numbers N and K.\r\n2. N represents the total number of soldiers standing in a circle having position marked from 0 to N-1.\r\n3. A cruel king wants to execute them but in a different way.\r\n4. He starts executing soldiers from 0th position and proceeds around the circle in clockwise direction.\r\n5. In each step, k-1 soldiers are skipped and the k-th soldier is executed.\r\n6. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed soldiers are removed), until only the last soldier remains, who is given freedom.\r\n7. You have to find the position of that lucky soldier.\r\n\r\nNote -> Check out the question video and write the recursive code as it is intended without changing signature. The judge can't force you but intends you to teach a concept.","inputFormat":"2 numbers N and K","outputFormat":"Check the sample ouput and question video.","constraints":"1 &lt;= N,K &lt;= 200","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static int solution(int n, int k){\r\n //write your code here\r\n return 0;\r\n }\r\n public static void main(String[] args){\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int k = scn.nextInt();\r\n System.out.println(solution(n,k));\r\n }\r\n \r\n\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"4\r\n2","sampleOutput":"0\r\n","questionVideo":"https://www.youtube.com/embed/dzYq5VEMZIg?start=0&end=162","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"082986ae-d618-4a59-9ab3-6d79056679a4","name":"Recursion and Backtracking For Intermediate","slug":"recursion-and-backtracking-for-intermediate-330","type":0},{"id":"d2e28fab-8444-42c8-a034-440f4edecc87","name":"Josephus Problem","slug":"josephus-problem","type":1}],"next":{"id":"dc12c637-2c3c-4fae-b5dc-7953d9cbe20f","name":"Josephus problems MCQ","type":0,"slug":"josephus-problems-mcq"},"prev":{"id":"527dd1f4-6960-4b2f-9edb-4d5ef02ebe56","name":"Max Score","type":3,"slug":"max-score"}}}`

# Josephus Problem

1. You are given two numbers N and K. 2. N represents the total number of soldiers standing in a circle having position marked from 0 to N-1. 3. A cruel king wants to execute them but in a different way. 4. He starts executing soldiers from 0th position and proceeds around the circle in clockwise direction. 5. In each step, k-1 soldiers are skipped and the k-th soldier is executed. 6. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed soldiers are removed), until only the last soldier remains, who is given freedom. 7. You have to find the position of that lucky soldier. Note -> Check out the question video and write the recursive code as it is intended without changing signature. The judge can't force you but intends you to teach a concept.

`{"id":"823c07f4-f71b-45f7-a9f3-29c2a2ee5fb2","name":"Josephus Problem","description":"1. You are given two numbers N and K.\r\n2. N represents the total number of soldiers standing in a circle having position marked from 0 to N-1.\r\n3. A cruel king wants to execute them but in a different way.\r\n4. He starts executing soldiers from 0th position and proceeds around the circle in clockwise direction.\r\n5. In each step, k-1 soldiers are skipped and the k-th soldier is executed.\r\n6. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed soldiers are removed), until only the last soldier remains, who is given freedom.\r\n7. You have to find the position of that lucky soldier.\r\n\r\nNote -> Check out the question video and write the recursive code as it is intended without changing signature. The judge can't force you but intends you to teach a concept.","inputFormat":"2 numbers N and K","outputFormat":"Check the sample ouput and question video.","constraints":"1 &lt;= N,K &lt;= 200","sampleCode":{"cpp":{"code":""},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main {\r\n\r\n public static int solution(int n, int k){\r\n //write your code here\r\n return 0;\r\n }\r\n public static void main(String[] args){\r\n Scanner scn = new Scanner(System.in);\r\n int n = scn.nextInt();\r\n int k = scn.nextInt();\r\n System.out.println(solution(n,k));\r\n }\r\n \r\n\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"4\r\n2","sampleOutput":"0\r\n","questionVideo":"https://www.youtube.com/embed/dzYq5VEMZIg?start=0&end=162","hints":[],"associated":[],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"082986ae-d618-4a59-9ab3-6d79056679a4","name":"Recursion and Backtracking For Intermediate","slug":"recursion-and-backtracking-for-intermediate-330","type":0},{"id":"d2e28fab-8444-42c8-a034-440f4edecc87","name":"Josephus Problem","slug":"josephus-problem","type":1}],"next":{"id":"dc12c637-2c3c-4fae-b5dc-7953d9cbe20f","name":"Josephus problems MCQ","type":0,"slug":"josephus-problems-mcq"},"prev":{"id":"527dd1f4-6960-4b2f-9edb-4d5ef02ebe56","name":"Max Score","type":3,"slug":"max-score"}}}`

Editor

# Josephus Problem

easy

1. You are given two numbers N and K. 2. N represents the total number of soldiers standing in a circle having position marked from 0 to N-1. 3. A cruel king wants to execute them but in a different way. 4. He starts executing soldiers from 0th position and proceeds around the circle in clockwise direction. 5. In each step, k-1 soldiers are skipped and the k-th soldier is executed. 6. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed soldiers are removed), until only the last soldier remains, who is given freedom. 7. You have to find the position of that lucky soldier. Note -> Check out the question video and write the recursive code as it is intended without changing signature. The judge can't force you but intends you to teach a concept.

1 <= N,K <= 200

## Format

### Input

2 numbers N and K

### Output

Check the sample ouput and question video.

## Example

Sample Input

```.css-23h8hz{color:inherit;font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;}4 2```

### Sample Output

```.css-3oaykw{color:var(--chakra-colors-active-primary);font-size:0.875rem;line-height:1.125rem;letter-spacing:0.016rem;font-weight:var(--chakra-fontWeights-normal);white-space:pre-wrap;font-family:Monospace;}0 ```

Question Video

Discussions

Show Discussion

Related Resources