fun List.reallocate(): List{ val (maxIndex, memorySize) = this.withIndex().maxBy { it.value }!! val reallocation = this.toMutableList() reallocation[maxIndex] = 0 (1..memorySize).map { (maxIndex + it)%this.size }.forEach { reallocation[it] += 1 } return reallocation } tailrec fun memoryAllocation(allocation: List, history: List> = listOf()) : Pair{ if (allocation in history){ return Pair(history.size, history.size - history.indexOf(allocation)) } return memoryAllocation(allocation.reallocate(), history.plusElement(allocation)) }