a few solutions
Sort the input array A, then perform a linear scan to find and return the minimal difference of each "k away" values i..i + k - 1 as the best answer.\n\n---\n\n
Sort the input array `A`, then perform a linear scan to find and return the minimal difference of each "`k` away" values `i..i + k - 1` as the `best` answer.\n\n---\n\n*Kotlin*\n```\nclass Solution {\n    fun minimumDifference(A: IntArray, k: Int): Int {\n        var best = (1e9 + 7).toInt()\n        A.sort()\n        for (i in 0..A.size - k)\n            best = Math.min(best, A[i + k - 1] - A[i])\n        return best\n    }\n}\n```\n\n*Javascript*\n```\nlet minimumDifference = (A, k, best = Infinity) => {\n    A.sort((a, b) => a - b);\n    for (let i = 0; i + k - 1 < A.length; ++i)\n        best = Math.min(best, A[i + k - 1] - A[i]);\n    return best;\n};\n```\n\n*Python3*\n```\nclass Solution:\n    def minimumDifference(self, A: List[int], K: int, best = float(\'inf\')) -> int:\n        A.sort()\n        for i in range(len(A) - K + 1):\n            best = min(best, A[i + K - 1] - A[i])\n        return best\n```\n\n*C++*\n```\nclass Solution {\npublic:\n    using VI = vector<int>;\n    int minimumDifference(VI& A, int K, int best = 1e9 + 7) {\n        sort(A.begin(), A.end());\n        for (auto i{ 0 }; i + K - 1 < A.size(); ++i)\n            best = min(best, A[i + K - 1] - A[i]);\n        return best;\n    }\n};\n```
[]