MCPcopy
hub / github.com/TheAlgorithms/Go / JosephusProblem

Function JosephusProblem

structure/linkedlist/cyclic.go:120–129  ·  view source on GitHub ↗

https://en.wikipedia.org/wiki/Josephus_problem This is a struct-based solution for Josephus problem.

(cl *Cyclic[int], k int)

Source from the content-addressed store, hash-verified

118// https://en.wikipedia.org/wiki/Josephus_problem
119// This is a struct-based solution for Josephus problem.
120func JosephusProblem(cl *Cyclic[int], k int) int {
121 for cl.Size > 1 {
122 cl.Rotate(k)
123 cl.Delete()
124 cl.Rotate(-1)
125 }
126 retval := cl.Head.Val
127 cl.Destroy()
128 return retval
129}

Callers 1

TestJosephusProblemFunction · 0.85

Calls 3

RotateMethod · 0.80
DestroyMethod · 0.80
DeleteMethod · 0.65

Tested by 1

TestJosephusProblemFunction · 0.68