import java.util.Arrays // Naive implementation, simulates the algorithm fun main() { val (n, k) = readLine()!!.split(" ").map(String::toInt) val windows = n - k val numbers = readLine()!!.split(" ").map(String::toLong).toLongArray() var iterations = 0 var changed: Boolean do { changed = false iterations++ (0 .. windows).forEach { offset -> // only sort if window isn't sorted yet val sort = numbers.asSequence().drop(offset).take(k).windowed(2).firstOrNull { (a, b) -> a > b } != null if (sort) { changed = true Arrays.sort(numbers, offset, offset + k) } } } while (changed) // will always do 1 iteration to many println(iterations - 1) }