Kotlin: O(1) memory
Code\n\nclass Solution {\n    fun findClosestElements(arr: IntArray, k: Int, x: Int): List<Int> {\n        var (l, r) = 0 to arr.size - k\n        \n        whi
# Code\n```\nclass Solution {\n    fun findClosestElements(arr: IntArray, k: Int, x: Int): List<Int> {\n        var (l, r) = 0 to arr.size - k\n        \n        while (l < r) {\n            val m = (l + r) / 2\n            if (x - arr[m] > arr[m + k] - x) l = m + 1\n            else r = m\n        }\n\n        return arr.slice(l until l + k)\n    }\n}\n```
['Binary Search', 'Sliding Window', 'Kotlin']