Okay, this is as fast and short as I can get it :s...
# announcements
o
Okay, this is as fast and short as I can get it simple smile
Copy code
class Item(val index: Int, var next: Item?)
private fun josephus3(size: Int, step: Int): Int {
    val last = Item(size - 1, null)
    val first = (size - 2 downTo 0).fold(last) { next, index -> Item(index, next) }
    last.next = first

    var alive = size
    var current = first
    var previous = last
    while (alive > 1) {
        for (i in 0..step - 1) {
            previous = current
            current = current.next!!
        }
        previous.next = current.next
        current = previous
        alive--
    }
    return current.index
}
👍 1