`{"id":"368c4fbe-0d24-492e-929b-b1c0664d1a74","name":"Josephus Special","description":"1. You are given an integer N which represents the total number of soldiers standing in a circle \r\n having position marked from 1 to N.\r\n2. A cruel king wants to execute them but in a different way.\r\n3. He starts executing soldiers from 1st position and proceeds around the circle in clockwise \r\n direction.\r\n4. In each step, every second soldier is executed.\r\n5. The elimination proceeds around the circle (which is becoming smaller and smaller as the \r\n executed soldiers are removed), until only the last soldier remains, who is given freedom.\r\n6. You have to find the position of that lucky soldier.","inputFormat":"A number N","outputFormat":"Check the sample ouput and question video.","constraints":"1 &lt;= N &lt;= 10^9","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n \n int solution(int n){\n \n }\n\n \n int main()\n {\n int n;\n cin>>n;\n cout<<solution(n);\n }"},"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){\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 System.out.println(solution(n));\r\n }\r\n \r\n\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"4","sampleOutput":"1\r\n","questionVideo":"https://www.youtube.com/embed/NjcMT9wZHV0?end=170","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":"f3e3dbef-d2b7-4f6d-b357-2ef3738e6c91","name":"Bit Manipulation For Intermediate","slug":"bit-manipulation-for-intermediate-9995","type":0},{"id":"f5b2a1fa-f99a-43e3-ad88-68b2aa50e1cf","name":"Josephus Special","slug":"josephus-special","type":1}],"next":{"id":"132bb489-c096-4809-b268-c1ea4919b124","name":"Josephus Special MCQ","type":0,"slug":"josephus-special-mcq"},"prev":{"id":"27e49318-44e1-41ad-ac39-e3de90d24768","name":"Kernighan’s Algorithm","type":3,"slug":"kernighan-s-algorithm"}}}`

# Josephus Special

1. You are given an integer N which represents the total number of soldiers standing in a circle having position marked from 1 to N. 2. A cruel king wants to execute them but in a different way. 3. He starts executing soldiers from 1st position and proceeds around the circle in clockwise direction. 4. In each step, every second soldier is executed. 5. 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. 6. You have to find the position of that lucky soldier.

`{"id":"368c4fbe-0d24-492e-929b-b1c0664d1a74","name":"Josephus Special","description":"1. You are given an integer N which represents the total number of soldiers standing in a circle \r\n having position marked from 1 to N.\r\n2. A cruel king wants to execute them but in a different way.\r\n3. He starts executing soldiers from 1st position and proceeds around the circle in clockwise \r\n direction.\r\n4. In each step, every second soldier is executed.\r\n5. The elimination proceeds around the circle (which is becoming smaller and smaller as the \r\n executed soldiers are removed), until only the last soldier remains, who is given freedom.\r\n6. You have to find the position of that lucky soldier.","inputFormat":"A number N","outputFormat":"Check the sample ouput and question video.","constraints":"1 &lt;= N &lt;= 10^9","sampleCode":{"cpp":{"code":"#include<bits/stdc++.h>\nusing namespace std;\n \n int solution(int n){\n \n }\n\n \n int main()\n {\n int n;\n cin>>n;\n cout<<solution(n);\n }"},"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){\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 System.out.println(solution(n));\r\n }\r\n \r\n\r\n}"},"python":{"code":""}},"points":10,"difficulty":"easy","sampleInput":"4","sampleOutput":"1\r\n","questionVideo":"https://www.youtube.com/embed/NjcMT9wZHV0?end=170","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":"f3e3dbef-d2b7-4f6d-b357-2ef3738e6c91","name":"Bit Manipulation For Intermediate","slug":"bit-manipulation-for-intermediate-9995","type":0},{"id":"f5b2a1fa-f99a-43e3-ad88-68b2aa50e1cf","name":"Josephus Special","slug":"josephus-special","type":1}],"next":{"id":"132bb489-c096-4809-b268-c1ea4919b124","name":"Josephus Special MCQ","type":0,"slug":"josephus-special-mcq"},"prev":{"id":"27e49318-44e1-41ad-ac39-e3de90d24768","name":"Kernighan’s Algorithm","type":3,"slug":"kernighan-s-algorithm"}}}`

Editor

# Josephus Special

easy

1. You are given an integer N which represents the total number of soldiers standing in a circle having position marked from 1 to N. 2. A cruel king wants to execute them but in a different way. 3. He starts executing soldiers from 1st position and proceeds around the circle in clockwise direction. 4. In each step, every second soldier is executed. 5. 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. 6. You have to find the position of that lucky soldier.

1 <= N <= 10^9

## Format

A number N

### 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`

### 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;}1 ```

Question Video

Discussions

Show Discussion

Related Resources