Questions tagged [josephus]

In computer science and mathematics, the Josephus Problem (or Josephus permutation) is a theoretical problem related to a certain counting-out game

Quoting the corresponding wikipedia page:

There are 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. The task is to choose the place in the initial circle so that you are the last one remaining and so survive.

71 questions
0
votes
1 answer

How does printing Josephus sequence work?

The CSES problem Josephus Problem I requires us to print the sequence of how people are chosen for n people and k = 2. I found an elegant solution to this here. Basically, the code is similar to this: void J(int n) { int a = 1, b = 0; while…
Ak01
  • 362
  • 3
  • 19
0
votes
3 answers

Circular array and elimination in C, how to return the last "living" element index?

I am trying to write a code that will simulate a circle of a fixed size of people that have one sword. The closest "living person" to the current index will be eliminated and the sword will be passed to the next living person (after the one who got…
NoobCoder
  • 513
  • 3
  • 18
0
votes
2 answers

I have an infinite for loop but I cant find which part of my code is causing it

I am trying to solve the Josephus problem with an ArrayList and for loops. I have created an infinite for loop within my circle.size for loop but I cant deduce which part of my code is causing it to happen. public class project1 { public static…
jelly_rex
  • 19
  • 3
0
votes
2 answers

I do not understand why I am seeing an index from my array list twice despite having removed it after the first occurrence

I am trying to solve the Josephus problem with an array list. I noticed that even though I remove and index after it is killed it still shows up in my output. Why does 2 appear again when it should have been removed? Below is my current…
jelly_rex
  • 19
  • 3
0
votes
1 answer

What's wrong in my approach in a problem of game of death in a circle?

#include #include #include using namespace std; void safe(int n,int k,int a[]) { vectors(a+0,a+n); vectorb(a+0,a+n); while(s.size()>1) { int r; r=s.size(); int…
0
votes
0 answers

Taking K'th element in BCT(Binary count Tree);

The problem with the function is that after 1 calling of it, it doesn't work as it should. For example, for k = 4, and size of a tree n = 7, when called it should return firstly node with value of 4 then 5, when in reality it return node with value…
Yashiru99
  • 43
  • 1
  • 7
0
votes
1 answer

I'm getting a math domain error in my Python code and have no clue what to do

I'm trying to do some code for the Josephus Problem, but I keep getting a weird error. My code looks like this: import math power = 2 originalnumber = int(input("how many to start?")) powerof2 = math.log(originalnumber,power) if type(powerof2) ==…
imtired
  • 11
  • 3
0
votes
1 answer

Josephus for-loop in Python

So, I tried to see if I could write code that would solve the Josephus Problem in Python for n amount of people (in the circle). I'm an amateur, so please bear with me. Here's the code I came up with it, but it has some problems. n =…
0
votes
2 answers

Josephus algorithm partial succes

My friend told me about Josephus problem, where you have 41 people sitting in the circle. Person number 1 has a sword, kills person on the right and passes the sword to the next person. This goes on until there is only one person left alive. I came…
piotrulu
  • 13
  • 1
  • 8
0
votes
0 answers

Python data structures-Josephus Circle algorithm

I was implementing Josephus circle algorithm in Python. I am having a small doubt related to an error which was coming when i wrote prev=Node(0) and head=Node(0). The error removed when I wrote head=Node(0) prev=head. Why the error was coming when I…
user6126396
0
votes
0 answers

Time Complexity of Advent of Code - 2016 Day 19

The problem for Advent of Code Day 19 is specified as follows: Elves numbered 1 to N sit in a circle. Each Elf brings a present. Then, starting with the first Elf, they take turns stealing all the presents from the Elf to their left. An Elf…
Solaxun
  • 2,732
  • 1
  • 22
  • 41
0
votes
1 answer

Josephus Algorithm Glitch using a circular linked list

I have to write a program that gives the output of the last man standing, according to the Josephus problem, utilizing a circular list. It seems to work the majority of the time, However, when I enter a series 7(people) 1(starting position), 3(kill…
0
votes
1 answer

Josephus Flavius on segment tree

I would like to solve Joseph Flavius problem using segment tree. I'm almost sure that simple simulation (i.e. lined list) is O(n^2). What I want to achieve is jumping on array for particular distance, taken from segment tree. In other words segment…
Michocio
  • 503
  • 3
  • 19
0
votes
2 answers

Josephus Algorithm

I was reading the algorithm for the Josephus Problem. I came across the following algorithm : int josephusIteration(int n,int k) { int a=1; for(int i=1;i<=n;i++) { a=(a+k-1)%i+1; } return a; } I couldn't understand its…
user5070412
0
votes
2 answers

Solving the Josephus Problem using a vector

EDIT: I seem to have sorted out the errors, at the very least, and have updated the code. However, the math still doesn't seem to be working out. Any ideas? In short, I'm trying to write a program in C++ that will prompt the user for the number of…
iSeven
  • 3
  • 4