Language: C++

#include <iostream>
#include <cassert>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <string>
#include <sstream>
#include <complex>
#include <vector>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <set>
using namespace std;


size_t solve(const vector<uint64_t>& heights, const size_t k)
{
    size_t min_height = INT_MAX;

    for(size_t i = 0; i < heights.size(); i++)
    {
        size_t j = i + (k - 1);
        if (j >= heights.size()) {
            break;
        }
        size_t diff = heights[j] - heights[i];
        min_height = min(min_height, diff);
    }

    return min_height;
}

void test_max_case()
{
    size_t n = pow(10, 5);
    size_t k = n / 2;
    vector<uint64_t> heights(n, UINT64_MAX);
    for(size_t i = 0; i < n; i++)
    {
        heights[i] = i + 1;
    }
    sort(heights.begin(), heights.end());

    size_t answer = solve(heights, k);
    cout << answer << endl;
}

int main ()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    size_t n, k = INT_MAX;
    cin >> n >> k;
    assert(2 <= n && n <= pow(10, 5));
    assert(2 <= k && k <= n);

    vector<uint64_t> heights(n, UINT64_MAX);
    for(size_t i = 0; i < n; i++)
    {
        uint64_t height = 0;
        cin >> height;
        assert(1 <= height && height <= pow(10, 9));
        heights[i] = height;
    }
    sort(heights.begin(), heights.end());

    size_t answer = solve(heights, k);

    cout << answer << endl;

    // test_max_case();

    return 0;
}

Compiler output:
a.cc:20:27: error: 'uint64_t' was not declared in this scope
   20 | size_t solve(const vector<uint64_t>& heights, const size_t k)
      |                           ^~~~~~~~
a.cc:17:1: note: 'uint64_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
   16 | #include <set>
  +++ |+#include <cstdint>
   17 | using namespace std;
a.cc:20:35: error: template argument 1 is invalid
   20 | size_t solve(const vector<uint64_t>& heights, const size_t k)
      |                                   ^
a.cc:20:35: error: template argument 2 is invalid
a.cc: In function 'size_t solve(const int&, size_t)':
a.cc:22:25: error: 'INT_MAX' was not declared in this scope
   22 |     size_t min_height = INT_MAX;
      |                         ^~~~~~~
a.cc:17:1: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
   16 | #include <set>
  +++ |+#include <climits>
   17 | using namespace std;
a.cc:24:35: error: request for member 'size' in 'heights', which is of non-class type 'const int'
   24 |     for(size_t i = 0; i < heights.size(); i++)
      |                                   ^~~~
a.cc:27:26: error: request for member 'size' in 'heights', which is of non-class type 'const int'
   27 |         if (j >= heights.size()) {
      |                          ^~~~
a.cc:30:30: error: invalid types 'const int[size_t {aka long unsigned int}]' for array subscript
   30 |         size_t diff = heights[j] - heights[i];
      |                              ^
a.cc:30:43: error: invalid types 'const int[size_t {aka long unsigned int}]' for array subscript
   30 |         size_t diff = heights[j] - heights[i];
      |                                           ^
a.cc: In function 'void test_max_case()':
a.cc:41:12: error: 'uint64_t' was not declared in this scope
   41 |     vector<uint64_t> heights(n, UINT64_MAX);
      |            ^~~~~~~~
a.cc:41:12: note: 'uint64_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
a.cc:41:20: error: template argument 1 is invalid
   41 |     vector<uint64_t> heights(n, UINT64_MAX);
      |                    ^
a.cc:41:20: error: template argument 2 is invalid
a.cc:41:33: error: 'UINT64_MAX' was not declared in this scope
   41 |     vector<uint64_t> heights(n, UINT64_MAX);
      |                                 ^~~~~~~~~~
a.cc:41:33: note: 'UINT64_MAX' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
a.cc:41:43: error: expression list treated as compound expression in initializer [-fpermissive]
   41 |     vector<uint64_t> heights(n, UINT64_MAX);
      |                                           ^
a.cc:44:16: error: invalid types 'int[size_t {aka long unsigned int}]' for array subscript
   44 |         heights[i] = i + 1;
      |                ^
a.cc:46:18: error: request for member 'begin' in 'heights', which is of non-class type 'int'
   46 |     sort(heights.begin(), heights.end());
      |                  ^~~~~
a.cc:46:35: error: request for member 'end' in 'heights', which is of non-class type 'int'
   46 |     sort(heights.begin(), heights.end());
      |                                   ^~~
a.cc: In function 'int main()':
a.cc:57:19: error: 'INT_MAX' was not declared in this scope
   57 |     size_t n, k = INT_MAX;
      |                   ^~~~~~~
a.cc:57:19: note: 'INT_MAX' is defined in header '<climits>'; this is probably fixable by adding '#include <climits>'
a.cc:62:12: error: 'uint64_t' was not declared in this scope
   62 |     vector<uint64_t> heights(n, UINT64_MAX);
      |            ^~~~~~~~
a.cc:62:12: note: 'uint64_t' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
a.cc:62:20: error: template argument 1 is invalid
   62 |     vector<uint64_t> heights(n, UINT64_MAX);
      |                    ^
a.cc:62:20: error: template argument 2 is invalid
a.cc:62:33: error: 'UINT64_MAX' was not declared in this scope
   62 |     vector<uint64_t> heights(n, UINT64_MAX);
      |                                 ^~~~~~~~~~
a.cc:62:33: note: 'UINT64_MAX' is defined in header '<cstdint>'; this is probably fixable by adding '#include <cstdint>'
a.cc:62:43: error: expression list treated as compound expression in initializer [-fpermissive]
   62 |     vector<uint64_t> heights(n, UINT64_MAX);
      |                                           ^
a.cc:65:17: error: expected ';' before 'height'
   65 |         uint64_t height = 0;
      |                 ^~~~~~~
      |                 ;
a.cc:66:16: error: 'height' was not declared in this scope; did you mean 'heights'?
   66 |         cin >> height;
      |                ^~~~~~
      |                heights
a.cc:68:16: error: invalid types 'int[size_t {aka long unsigned int}]' for array subscript
   68 |         heights[i] = height;
      |                ^
a.cc:70:18: error: request for member 'begin' in 'heights', which is of non-class type 'int'
   70 |     sort(heights.begin(), heights.end());
      |                  ^~~~~
a.cc:70:35: error: request for member 'end' in 'heights', which is of non-class type 'int'
   70 |     sort(heights.begin(), heights.end());
      |                                   ^~~