Skip to content

PYTHON PROBLEM #348

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 97 additions & 0 deletions PYTHON PROBLEM
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
Question:There are N people standing in a circle waiting to be executed. The counting out begins at some
point in the circle and proceeds around the circle in a fixed direction. In each step, a certain number
of people are skipped and the next person is executed. The elimination proceeds around the circle
(which is becoming smaller and smaller as the executed people are removed), until only the last person remains, who is given freedom.

Given the total number of persons N and a number k which indicates that k-1 persons are skipped and the kth person is killed in a circle.
The task is to choose the person in the initial circle that survives.


Example:Input: N = 5 and k = 2
Output: 3
Explanation: Firstly, the person at position 2 is killed,
then the person at position 4 is killed, then the person at position 1 is killed.
Finally, the person at position 5 is killed. So the person at position 3 survives.


Input: N = 7 and k = 3
Output: 4
Explanations: The persons at positions 3, 6, 2, 7, 5, and 1 are killed in order,
and the person at position 4 survives.


Code in Python Language:

def Josh(person, k, index):

# Base case , when only one person is left
if len(person) == 1:
print(person[0])
return

# find the index of first person which will die
index = ((index+k)%len(person))

# remove the first person which is going to be killed
person.pop(index)

# recursive call for n-1 persons
Josh(person,k,index)

# Driver Program to test above function
n = 14 # specific n and k values for original josephus problem
k = 2
k-=1 # (k-1)th person will be killed

index = 0

# fill the person vector
person=[]
for i in range(1,n+1):
person.append(i)

Josh(person,k,index)



CODE IN C++

#include <bits/stdc++.h>

using namespace std;

void Josh(vector<int> person, int k, int index)
{
// Base case , when only one person is left
if (person.size() == 1) {
cout << person[0] << endl;
return;
}

// find the index of first person which will die
index = ((index + k) % person.size());

// remove the first person which is going to be killed
person.erase(person.begin() + index);

// recursive call for n-1 persons
Josh(person, k, index);
}

int main()
{
int n = 14; // specific n and k values for original
// josephus problem
int k = 2;
k--; // (k-1)th person will be killed
int index
= 0; // The index where the person which will die

vector<int> person;
// fill the person vector
for (int i = 1; i <= n; i++) {
person.push_back(i);
}

Josh(person, k, index);
}