prithivMLmods/Coder-Stat huggingface dataset:
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

int main() {
  int N, K;
  long min = 10000000000000;
  cin >> N;
  cin >> K;
  vector<int> h(N);

  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }

  sort(h.begin(), h.end());
  for (int i = 0; i < N; i++) {
    if (min > abs(h[K - 1 + i] - h[i])) {
      min = abs(h[K - 1 + i] - h[i]);
    }
  }

  cout << min << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <stdio.h>
#include <string>
#include <vector>

using namespace std;

int main() {
  int N, K;
  long min = 10000000000000;
  cin >> N;
  cin >> K;
  vector<int> h(N);

  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }

  sort(h.begin(), h.end());
  for (int i = 0; N - K + 1 != i; i++) {
    if (min > abs(h[K - 1 + i] - h[i])) {
      min = abs(h[K - 1 + i] - h[i]);
    }
  }

  cout << min << endl;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
const int INF = 1 << 30;
using namespace std;

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

  int n, k;
  cin >> n >> k;
  vector<int> h(n);
  rep(i, n) cin >> h[i];
  sort(h.begin(), h.end());

  int ans = INF;
  rep(i, n - 2) { ans = min(ans, h[i + k - 1] - h[i]); }
  cout << ans << '\n';
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; ++i)
typedef long long ll;
const int INF = 1 << 30;
using namespace std;

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

  int n, k;
  cin >> n >> k;
  vector<int> h(n);
  rep(i, n) cin >> h[i];
  sort(h.begin(), h.end());

  int ans = INF;
  rep(i, n - k + 1) { ans = min(ans, h[i + k - 1] - h[i]); }
  cout << ans << '\n';
  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  std::vector<int> h;
  for (int i = 0; i < n; ++i) {
    int tmp;
    cin >> tmp;
    h.push_back(tmp);
  }
  std::sort(h.begin(), h.end());
  int min = h.at(n - 1);
  for (int i = 0; i < n - (k - 1); ++i) {
    int tmp = h.at(n + k - 1) - h.at(n);
    if (min > tmp)
      min = tmp;
  }
  // 出力
  cout << min << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int n, k;
  cin >> n >> k;
  std::vector<int> h;
  for (int i = 0; i < n; ++i) {
    int tmp;
    cin >> tmp;
    h.push_back(tmp);
  }
  std::sort(h.begin(), h.end());
  int min = h.at(n - 1);
  for (int i = 0; i < n - (k - 1); ++i) {
    int tmp = h.at(i + k - 1) - h.at(i);
    if (min > tmp)
      min = tmp;
  }
  // 出力
  cout << min << endl;
  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T, class C> void chmax(T &a, C b) { a > b ?: a = b; }
template <class T, class C> void chmin(T &a, C b) { a < b ?: a = b; }
int main(int argc, char *argv[]) {
  unsigned long n, k;
  cin >> n >> k;
  vector<unsigned long> h(n);
  rep(i, n) cin >> h[i];
  sort(ALL(h));
  unsigned long min = pow(10, 9);
  rep(i, n) {
    unsigned long t = h[i + k - 1] - h[i];
    if (min > t)
      min = t;
  }
  cout << min << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for (int i = x; i < n; i++)
template <class T, class C> void chmax(T &a, C b) { a > b ?: a = b; }
template <class T, class C> void chmin(T &a, C b) { a < b ?: a = b; }
int main(int argc, char *argv[]) {
  unsigned long n, k;
  cin >> n >> k;
  vector<unsigned long> h(n);
  rep(i, n) cin >> h[i];
  sort(ALL(h));
  unsigned long min = pow(10, 9);
  rep(i, n - k + 1) {
    unsigned long t = h[i + k - 1] - h[i];
    if (min > t)
      min = t;
  }
  cout << min << endl;
  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#pragma GCC target("avx2,tune=native")

#include <bits/stdc++.h>
#include <x86intrin.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int N, K;
  cin >> N >> K;
  vector<int> h(N);
  for (auto &i : h)
    cin >> i;
  sort(h.begin(), h.end());
  int res = INT_MAX;
  for (int i = 0; i < N - K + 1; i++) {
    int foo = h[K + i - 1] - h[i];
    if (foo < res)
      res = foo;
  }
  cout << res;
  return 0;
}

Fixed submission:
#pragma GCC optimize("Ofast")
#pragma GCC target("tune=native")

#include <bits/stdc++.h>
#include <x86intrin.h>

using namespace std;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  int N, K;
  cin >> N >> K;
  vector<int> h(N);
  for (auto &i : h)
    cin >> i;
  sort(h.begin(), h.end());
  int res = INT_MAX;
  for (int i = 0; i < N - K + 1; i++) {
    int foo = h[K + i - 1] - h[i];
    if (foo < res)
      res = foo;
  }
  cout << res;
  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const long long INF = 1LL << 60;

int main() {
  int N, K;
  cin >> N >> K;
  vector<long long> h(N);
  for (int i = 0; i < N; i++)
    cin >> h[i];

  sort(h.begin(), h.end());
  long long ans = INF;

  for (int i = 0; i <= N - K; i++) {
    ans = min(ans, h[i - K + 1] - h[i]);
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

const long long INF = 1LL << 60;

int main() {
  int N, K;
  cin >> N >> K;
  vector<long long> h(N);
  for (int i = 0; i < N; i++)
    cin >> h[i];

  sort(h.begin(), h.end());
  long long ans = INF;

  for (int i = 0; i <= N - K; i++) {
    ans = min(ans, h[i + K - 1] - h[i]);
  }

  cout << ans << endl;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
using Graph = vector<vector<int>>;
#define MOD 1000000007
#define MOD2 998244353
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;

vector<ll> h;

int main() {
  int n, k;
  cin >> n >> k;
  rep(i, n) {
    ll t;
    cin >> t;
    h.push_back(t);
  }
  sort(h.begin(), h.end());

  ll ans = LINF;

  rep(i, k) { ans = min(ans, h[k + i - 1] - h[i]); }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n - 1; i >= 0; i--)
using namespace std;
using Graph = vector<vector<int>>;
#define MOD 1000000007
#define MOD2 998244353
#define INF ((1 << 30) - 1)
#define LINF (1LL << 60)
#define EPS (1e-10)
typedef long long ll;
typedef pair<ll, ll> P;

vector<ll> h;

int main() {
  int n, k;
  cin >> n >> k;
  rep(i, n) {
    ll t;
    cin >> t;
    h.push_back(t);
  }
  sort(h.begin(), h.end());

  ll ans = LINF;

  rep(i, n - k + 1) { ans = min(ans, h[k + i - 1] - h[i]); }

  cout << ans << endl;

  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
// #include <bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int N, K;
  cin >> N >> K;
  vector<int> h(N);
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  sort(h.begin(), h.end());
  int min_hight = 1e9;
  for (int i = 0; i < N; i++) {
    min_hight = min(min_hight, h[i + K - 1] - h[i]);
  }
  cout << min_hight << endl;
  return 0;
}


Fixed submission:
// #include <bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);

  int N, K;
  cin >> N >> K;
  vector<int> h(N);
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  sort(h.begin(), h.end());
  int min_hight = 1e9;
  for (int i = 0; i < N - K + 1; i++) {
    min_hight = min(min_hight, h[i + K - 1] - h[i]);
  }
  cout << min_hight << endl;
  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep11(i, n) for (int i = 1; i < (int)(n); ++i)
#define repo(i, o, n) for (int i = o; i < (int)(n); ++i)
#define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pb(n) push_back(n)
#define mp make_pair
#define MOD 1002000007
#define INF LONG_LONG_MAX

typedef pair<int, int> P;
int main() {
  long long n, k;
  cin >> n >> k;
  vector<int> v;
  vector<int> w;
  rep(i, n) {
    long long h;
    cin >> h;
    v.push_back(h);
  }
  sort(v.begin(), v.end());
  rep(i, n - (k - 1)) { w[i] = v[i + (k - 1)] - v[i]; }
  sort(w.begin(), w.end());
  cout << w[0] << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define rep11(i, n) for (int i = 1; i < (int)(n); ++i)
#define repo(i, o, n) for (int i = o; i < (int)(n); ++i)
#define repm(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define pb(n) push_back(n)
#define mp make_pair
#define MOD 1002000007
#define INF LONG_LONG_MAX

typedef pair<int, int> P;
int main() {
  long long n, k;
  cin >> n >> k;
  vector<int> v;
  vector<int> w(n - (k - 1));
  rep(i, n) {
    long long h;
    cin >> h;
    v.push_back(h);
  }
  sort(v.begin(), v.end());
  rep(i, n - (k - 1)) { w[i] = v[i + (k - 1)] - v[i]; }
  sort(w.begin(), w.end());
  cout << w[0] << endl;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)

int main(void) {
  int n, k;
  cin >> n >> k;

  ll h[n];
  rep(i, n) { cin >> h[i]; }

  sort(h, h + n);

  ll diff = 10000000;
  rep(i, n) { diff = min(h[i + k - 1] - h[i], diff); }

  cout << diff << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)

int main(void) {
  int n, k;
  cin >> n >> k;

  ll h[n];
  rep(i, n) { cin >> h[i]; }

  sort(h, h + n);

  ll diff = 10000000000;
  rep(i, n) {
    if (i + k - 1 == n)
      break;
    diff = min(h[i + k - 1] - h[i], diff);
  }

  cout << diff << endl;
  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
  int N, K;
  std::cin >> N >> K;
  std::vector<int> h(N);
  for (int i = 0; i < N; i++) {
    std::cin >> h.at(i);
  }
  std::sort(h.begin(), h.end());
  int min = 1000000000;
  for (int i = 0; i < N + K - 1; i++) {
    int temp = h.at(i + K - 1) - h.at(i);
    if (temp < min) {
      min = temp;
    }
  }
  std::cout << min << std::endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

int main() {
  int N, K;
  std::cin >> N >> K;
  std::vector<int> h(N);
  for (int i = 0; i < N; i++) {
    std::cin >> h.at(i);
  }
  std::sort(h.begin(), h.end());
  int min = 1000000000;
  for (int i = 0; i < N - K + 1; i++) {
    int temp = h.at(i + K - 1) - h.at(i);
    if (temp < min) {
      min = temp;
    }
  }
  std::cout << min << std::endl;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
typedef long long ll;
typedef pair<int, int> P;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> c(n);
  rep(i, 0, n) { cin >> c[i]; }
  sort(c.begin(), c.end());
  int ans = c[n - k] - c[0];
  rep(i, 1, n - k + 1) { ans = min(c[i + n - k] - c[i], ans); }

  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define rep(i, a, b) for (int i = a; i < (b); ++i)
typedef long long ll;
typedef pair<int, int> P;
int main() {
  int n, k;
  cin >> n >> k;
  vector<int> c(n);
  rep(i, 0, n) { cin >> c[i]; }
  sort(c.begin(), c.end());
  int ans = 1000000001;
  rep(i, 0, n - k + 1) { ans = min(c[i + k - 1] - c[i], ans); }

  cout << ans << endl;
  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int compare_lld(const void *a, const void *b) {
  long long int *A = (long long int *)a;
  long long int *B = (long long int *)b;
  if (*A > *B)
    return 1;
  if (*A < *B)
    return -1;
  return 0;
}

int main() {
  long long int n, k, i;
  scanf("%lld", &n);
  scanf("%lld", &k);
  long long int h[n];
  for (i = 0; i < n; i++) {
    scanf("%lld", &h[i]);
  }
  qsort(h, n, sizeof(long long int), compare_lld);
  long long int ans = h[n - 1] - h[0];
  long long int temp = 0;
  for (i = 0; i < n; i++) {
    temp = h[i + k - 1] - h[i];
    if (temp < ans) {
      ans = temp;
    }
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int compare_lld(const void *a, const void *b) {
  long long int *A = (long long int *)a;
  long long int *B = (long long int *)b;
  if (*A > *B)
    return 1;
  if (*A < *B)
    return -1;
  return 0;
}

int main() {
  long long int n, k, i;
  scanf("%lld", &n);
  scanf("%lld", &k);
  long long int h[n];
  for (i = 0; i < n; i++) {
    scanf("%lld", &h[i]);
  }
  qsort(h, n, sizeof(long long int), compare_lld);
  long long int ans = h[n - 1] - h[0];
  long long int temp = 0;
  for (i = 0; i + k - 1 < n; i++) {
    temp = h[i + k - 1] - h[i];
    if (temp < ans) {
      ans = temp;
    }
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair

using ll = long long;
using namespace std;
ll x, y, z, l, m, n, r, q, k;
// 0 1 2 3 4 5 6 n = 7;
void solve() {
  cin >> n >> k;
  ll a[1000];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll md = INT_MAX;
  sort(a, a + n);
  for (int i = 0; i <= n - k; i++) {
    md = min(md, a[i + k - 1] - a[i]);
  }
  cout << md << '\n';
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(NULL);
  int test;
  test = 1;
  while (test--) {
    solve();
  }
  return 0;
}

Fixed submission:
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair

using ll = long long;
using namespace std;
ll x, y, z, l, m, n, r, q, k;
// 0 1 2 3 4 5 6 n = 7;
void solve() {
  cin >> n >> k;
  ll a[100000];
  for (int i = 0; i < n; i++)
    cin >> a[i];
  ll md = INT_MAX;
  sort(a, a + n);
  for (int i = 0; i <= n - k; i++) {
    md = min(md, a[i + k - 1] - a[i]);
  }
  cout << md << '\n';
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(NULL);
  int test;
  test = 1;
  while (test--) {
    solve();
  }
  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, k, a;
  cin >> n >> k;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    cin >> a;
    v.push_back(a);
  }
  sort(v.begin(), v.end());
  int diff = INT_MAX;
  for (int i = 0; i < n; i++) {
    diff = min(diff, v[i + k - 1] - v[i]);
  }
  cout << diff << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, k, a;
  cin >> n >> k;
  vector<int> v;
  for (int i = 0; i < n; i++) {
    cin >> a;
    v.push_back(a);
  }
  sort(v.begin(), v.end());
  int diff = INT_MAX;
  for (int i = 0; i <= n - k; i++) {
    diff = min(diff, v[i + k - 1] - v[i]);
  }
  cout << diff << endl;
  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fst first
#define snd second
#define rep(n) for (lint I = 0; (I) < (lint)(n); ++(I))
#define repeat(i, n) for (lint i = 0; (i) < (lint)(n); ++(i))
#define repeat_to(i, n) for (lint i = 0; (i) <= (lint)(n); ++(i))
#define repeat_from(i, m, n) for (lint i = (m); (i) < (lint)(n); ++(i))
#define repeat_from_to(i, m, n) for (lint i = (m); (i) <= (lint)(n); ++(i))
#define repeat_reverse_from_to(i, m, n)                                        \
  for (lint i = (m); (i) >= (lint)(n); --(i))
#define el cout << endl
#define dump(x) cout << " " << #x << "=" << x
#define pdump(p) cout << " " << #p << "=(" << p.fst << "," << p.snd << ")"
#define vdump(v)                                                               \
  for (size_t I = 0; I < v.size(); ++I) {                                      \
    cout << " " << #v << "[" << I << "]=" << v[I];                             \
  }                                                                            \
  cout << endl
using namespace std;
using lint = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<lint, lint>;

int main(void) {
  int n, k;
  cin >> n >> k;
  vector<int> h(n);
  repeat(i, n) cin >> h[i];
  sort(h.begin(), h.end());

  int ans = 1e9 + 1;
  repeat(i, n - k + 1) {
    // cout << "h[" << n-k+i << "]-h[" << i << "]" << endl;
    ans = min(ans, h[n - k + i] - h[i]);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define fst first
#define snd second
#define rep(n) for (lint I = 0; (I) < (lint)(n); ++(I))
#define repeat(i, n) for (lint i = 0; (i) < (lint)(n); ++(i))
#define repeat_to(i, n) for (lint i = 0; (i) <= (lint)(n); ++(i))
#define repeat_from(i, m, n) for (lint i = (m); (i) < (lint)(n); ++(i))
#define repeat_from_to(i, m, n) for (lint i = (m); (i) <= (lint)(n); ++(i))
#define repeat_reverse_from_to(i, m, n)                                        \
  for (lint i = (m); (i) >= (lint)(n); --(i))
#define el cout << endl
#define dump(x) cout << " " << #x << "=" << x
#define pdump(p) cout << " " << #p << "=(" << p.fst << "," << p.snd << ")"
#define vdump(v)                                                               \
  for (size_t I = 0; I < v.size(); ++I) {                                      \
    cout << " " << #v << "[" << I << "]=" << v[I];                             \
  }                                                                            \
  cout << endl
using namespace std;
using lint = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<lint, lint>;

int main(void) {
  int n, k;
  cin >> n >> k;
  vector<int> h(n);
  repeat(i, n) cin >> h[i];
  sort(h.begin(), h.end());

  int ans = 1e9 + 1;
  repeat(i, n - k + 1) {
    // cout << "h[" << i+k-1 << "]-h[" << i << "]=" << h[i+k-1] - h[i] << endl;
    ans = min(ans, h[i + k - 1] - h[i]);
  }
  cout << ans << endl;
}
Problem p03208 (C++)

Buggy submission (status: Time Limit Exceeded):
// lcmとかはg++ -std=c++17 default.cppみたいなかんじで

#include <bits/stdc++.h>
#define mod 1000000007
#define INF 1001001001
#define ll long long
#define ln cout << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define double long double
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define rep1(i, n) REP(i, 1, n + 1)
using namespace std;

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

  ll a, b, c, d, m, n, maxi = 0, f = 0, mini = INF, sum = 0;
  string str;
  cin >> n >> m;
  // vector<vector<ll>> v(tate,vector<ll> (yoko));
  vector<ll> v(n);
  rep(i, n) cin >> v[i];
  sort(v.begin(), v.end(), greater<ll>());
  vector<ll> w(n - 1);
  rep(i, n - 1) { w[i] = v[i] - v[i + 1]; }

  vector<ll> x(n - m + 1);
  rep(i, n - m + 1) {
    for (ll j = i; j < i + m - 1; j++) {
      x[i] += w[j];
    }
    // cout<<x[i]<<endl;
  }
  sort(x.begin(), x.end());
  cout << x[0] << endl;

  return 0;
}


Fixed submission:
// lcmとかはg++ -std=c++17 default.cppみたいなかんじで

#include <bits/stdc++.h>
#define mod 1000000007
#define INF 1001001001
#define ll long long
#define ln cout << endl
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define double long double
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define rep1(i, n) REP(i, 1, n + 1)
using namespace std;

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

  ll a, b, c, d, m, n, maxi = 0, f = 0, mini = INF, sum = 0;
  string str;
  cin >> n >> m;
  // vector<vector<ll>> v(tate,vector<ll> (yoko));
  vector<ll> v(n);
  rep(i, n) cin >> v[i];
  sort(v.begin(), v.end(), greater<ll>());
  vector<ll> w(n - 1);
  rep(i, n - 1) { w[i] = v[i] - v[i + 1]; }

  vector<ll> x(n - m + 1);
  rep(i, n - m + 1) { x[i] = v[i] - v[i + m - 1]; }
  sort(x.begin(), x.end());
  cout << x[0] << endl;

  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <stdio.h>
using namespace std;

int abs(int n) { return n >= 0 ? n : -n; }

int main() {
  int n, k;
  int h[10000];
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++)
    scanf("%d", &h[i]);
  sort(h, h + n);
  int min = 1000000000;
  for (int i = 0; i < n - k + 1; i++) {
    if (min > abs(h[i] - h[i + k - 1]))
      min = abs(h[i] - h[i + k - 1]);
  }
  printf("%d\n", min);
}


Fixed submission:
#include <algorithm>
#include <stdio.h>
using namespace std;

int abs(int n) { return n >= 0 ? n : -n; }

int main() {
  int n, k;
  int h[100000];
  scanf("%d %d", &n, &k);
  for (int i = 0; i < n; i++)
    scanf("%d", &h[i]);
  sort(h, h + n);
  int min = 1000000000;
  for (int i = 0; i < n - k + 1; i++) {
    if (min > abs(h[i] - h[i + k - 1]))
      min = abs(h[i] - h[i + k - 1]);
  }
  printf("%d\n", min);
}
Problem p03208 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  ll h[n];
  for (int i = 0; i < n; i++)
    cin >> h[i];
  sort(h, h + n);
  ll ans = 1e9 + 1;
  for (int i = 0; i < n - k + 1; i++) {
    ll hmin = 1e9 + 1;
    ll hmax = -1;
    for (int j = i; j < i + k; j++) {
      hmin = min(hmin, h[j]);
      hmax = max(hmax, h[j]);
    }
    ans = min(ans, hmax - hmin);
  }
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  ll h[n];
  for (int i = 0; i < n; i++)
    cin >> h[i];
  sort(h, h + n);
  ll ans = 1e9 + 1;
  for (int i = 0; i < n; i++) {
    if (i + k - 1 > n - 1)
      continue;
    ll tmp = h[i + k - 1] - h[i];
    ans = min(ans, tmp);
  }
  cout << ans << endl;
  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define REPR(i, n) for (int(i) = (n); (i) >= 0; (i)--)
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define ALL(x) x.begin(), x.end()
#define REVERSE(x) reverse(x.begin(), x.end())
#define SORT(x, n) sort(x, x)
#define P pair<int, int>
typedef long long LL;
const int INF = 1000000007;
using namespace std;
int gcd(int a, int b) {
  if (a % b == 0) {
    return b;
  } else {
    return gcd(b, a % b);
  }
}
int main() {
  int N, K;
  cin >> N >> K;
  vector<int> h(N);
  REP(i, N) { cin >> h[i]; }
  sort(ALL(h));
  int ans = INF;
  REP(i, N - K + 1) {
    int maxh = 0;
    int minh = INF;
    for (int j = i; j < i + K; j++) {
      maxh = max(maxh, h[j]);
      minh = min(minh, h[j]);
    }
    ans = min(ans, (maxh - minh));
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define REPR(i, n) for (int(i) = (n); (i) >= 0; (i)--)
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define ALL(x) x.begin(), x.end()
#define REVERSE(x) reverse(x.begin(), x.end())
#define SORT(x, n) sort(x, x)
#define P pair<int, int>
typedef long long LL;
const int INF = 1000000007;
using namespace std;
int gcd(int a, int b) {
  if (a % b == 0) {
    return b;
  } else {
    return gcd(b, a % b);
  }
}
int main() {
  int N, K;
  cin >> N >> K;
  vector<int> h(N);
  REP(i, N) { cin >> h[i]; }
  sort(ALL(h));
  int ans = INF;
  REP(i, N - K + 1) { ans = min(ans, h[i + K - 1] - h[i]); }
  cout << ans;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define INF 1e9
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(x) (x).begin(), (x).end()    // 昇順
#define RALL(x) (x).rbegin(), (x).rend() // 降順
const long long mod = 1e9 + 7;
typedef priority_queue<int> PQ;
typedef vector<long long> VL;
typedef vector<int> VI; // VI a(n);
typedef vector<string> VS;
typedef vector<char> VC;
typedef vector<VS> VSS;
typedef vector<VC> VCC;
typedef vector<VI> VII; // VII a(n,vector<int>(m)) n * m
typedef vector<VL> VLL;
typedef pair<int, int> PII;
typedef map<int, int> MP; // MP a;
typedef vector<pair<int, int>> PS;
typedef long long ll; // ll とdoubleは違う
template <class T>    // chmax(max, a);
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> // chmin(min,a)
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> // operator << (cout,a);
ostream &operator<<(ostream &os, vector<T> &v) {
  os << "{";
  rep(i, (int)v.size()) { os << v[i] << (i < v.size() - 1 ? ", " : ""); }
  os << "}";
  return os;
}

ll Max = -1, Min = 1e9 + 1;

int main() {
  ll b, x = 0, y = 0, n, m, k, w, sum = 0;
  string s, t;
  bool f = true;
  cin >> n >> k;
  VL a(n);
  rep(i, n) { cin >> a[i]; }
  sort(ALL(a));
  rep(i, n) {
    if (Min > a[i + k - 1] - a[i])
      Min = a[i + k - 1] - a[i];
  }
  cout << Min << endl;
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define INF 1e9
#define rep(i, n) for (int i = 0; i < n; i++)
#define ALL(x) (x).begin(), (x).end()    // 昇順
#define RALL(x) (x).rbegin(), (x).rend() // 降順
const long long mod = 1e9 + 7;
typedef priority_queue<int> PQ;
typedef vector<long long> VL;
typedef vector<int> VI; // VI a(n);
typedef vector<string> VS;
typedef vector<char> VC;
typedef vector<VS> VSS;
typedef vector<VC> VCC;
typedef vector<VI> VII; // VII a(n,vector<int>(m)) n * m
typedef vector<VL> VLL;
typedef pair<int, int> PII;
typedef map<int, int> MP; // MP a;
typedef vector<pair<int, int>> PS;
typedef long long ll; // ll とdoubleは違う
template <class T>    // chmax(max, a);
bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> // chmin(min,a)
bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> // operator << (cout,a);
ostream &operator<<(ostream &os, vector<T> &v) {
  os << "{";
  rep(i, (int)v.size()) { os << v[i] << (i < v.size() - 1 ? ", " : ""); }
  os << "}";
  return os;
}

ll Max = -1, Min = 1e9 + 1;

int main() {
  ll b, x = 0, y = 0, n, m, k, w, sum = 0;
  string s, t;
  bool f = true;
  cin >> n >> k;
  VL a(n);
  rep(i, n) { cin >> a[i]; }
  sort(ALL(a));
  rep(i, n - k + 1) {
    if (Min > a[i + k - 1] - a[i])
      Min = a[i + k - 1] - a[i];
  }
  cout << Min << endl;
  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
const int INF = 1e9;
const long long INFL = 1e18;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

using namespace std;

#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i <= b; i++)
#define all(a) begin((a)), end((a))
#define uniq(c) (c).erase(unique(all((c))), end((c)))

int main() {
  long long N, K;
  cin >> N >> K;
  --K;
  vector<long long> h(N);
  rep(i, 0, N) cin >> h[i];

  sort(h.begin(), h.end(), greater<>());
  long long ans = INFL;
  for (long long i = 0; i < h.size(); i += K) {
    ans = min(ans, h[i] - h[i + K]);
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
const int INF = 1e9;
const long long INFL = 1e18;
const int MOD = 1e9 + 7;
const ll LINF = 1e18;

using namespace std;

#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, a, b) for (int i = a; i <= b; i++)
#define all(a) begin((a)), end((a))
#define uniq(c) (c).erase(unique(all((c))), end((c)))

int main() {
  long long N, K;
  cin >> N >> K;
  --K;
  vector<long long> h(N);
  rep(i, 0, N) cin >> h[i];

  sort(h.begin(), h.end(), greater<>());
  long long ans = INFL;
  for (long long i = 0; i < h.size() - K; i++) {
    ans = min(ans, h[i] - h[i + K]);
  }

  cout << ans << endl;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
// AtCoder Beginner Contest 115 - C.cpp
#include <algorithm>
#include <cstdio>
#include <iostream>
const int maxn = 10020;
using namespace std;
int n, arr[maxn], K;
int main() {
  scanf("%d%d", &n, &K);
  for (int i = 1; i <= n; i++)
    scanf("%d", &arr[i]);
  sort(arr + 1, arr + 1 + n);
  int min_val = 1e9;
  for (int i = K; i <= n; i++)
    min_val = min(min_val, arr[i] - arr[i - K + 1]);
  printf("%d", min_val);
  return 0;
}

Fixed submission:
// AtCoder Beginner Contest 115 - C.cpp
#include <algorithm>
#include <cstdio>
#include <iostream>
const int maxn = 100200;
using namespace std;
int n, arr[maxn], K;
int main() {
  scanf("%d%d", &n, &K);
  for (int i = 1; i <= n; i++)
    scanf("%d", &arr[i]);
  sort(arr + 1, arr + 1 + n);
  int min_val = 1e9;
  for (int i = K; i <= n; i++)
    min_val = min(min_val, arr[i] - arr[i - K + 1]);
  printf("%d", min_val);
  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, K;
  long long H = 1000000001;
  cin >> N >> K;
  vector<long long> vec(N);
  for (int X = 0; X < N; X++) {
    cin >> vec.at(X);
  }
  sort(vec.begin(), vec.end());
  for (int X = 0; X < N; X++) {
    if (vec.at(K + X - 1) - vec.at(X) < H) {
      H = vec.at(K + X - 1) - vec.at(X);
    }
  }
  cout << H << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N, K;
  long long H = 1000000001;
  cin >> N >> K;
  vector<long long> vec(N);
  for (int X = 0; X < N; X++) {
    cin >> vec.at(X);
  }
  sort(vec.begin(), vec.end());
  for (int X = 0; K + X - 1 < N; X++) {
    if (vec.at(K + X - 1) - vec.at(X) < H) {
      H = vec.at(K + X - 1) - vec.at(X);
    }
  }
  cout << H << endl;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define isNum(a) (a >= '0' && a <= '9')
#define SP putchar(' ')
#define EL putchar('\n')
#define inf 2147483647
#define N 10005
#define File(a) freopen(a ".in", "r", stdin), freopen(a ".out", "w", stdout)
template <class T1> inline void read(T1 &);
template <class T1> inline void write(T1);
int a[N];
int main() {
  int n, k;
  read(n);
  read(k);
  for (int i = 1; i <= n; ++i) {
    read(a[i]);
  }
  sort(a + 1, a + n + 1);
  int ans = inf;
  for (int i = 1; i <= n - k + 1; ++i) {
    ans = min(ans, a[i + k - 1] - a[i]);
  }
  write(ans);
  EL;
  return 0;
}
template <class T1> void read(T1 &r_e_a_d) {
  T1 k = 0;
  char ch = getchar();
  int flag = 1;
  while (!isNum(ch)) {
    if (ch == '-') {
      flag = -1;
    }
    ch = getchar();
  }
  while (isNum(ch)) {
    k = (k << 1) + (k << 3) + ch - '0';
    ch = getchar();
  }
  r_e_a_d = flag * k;
}
template <class T1> void write(T1 w_r_i_t_e) {
  if (w_r_i_t_e < 0) {
    putchar('-');
    write(-w_r_i_t_e);
  } else {
    if (w_r_i_t_e < 10) {
      putchar(w_r_i_t_e + '0');
    } else {
      write(w_r_i_t_e / 10);
      putchar((w_r_i_t_e % 10) + '0');
    }
  }
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define isNum(a) (a >= '0' && a <= '9')
#define SP putchar(' ')
#define EL putchar('\n')
#define inf 2147483647
#define N 100005
#define File(a) freopen(a ".in", "r", stdin), freopen(a ".out", "w", stdout)
template <class T1> inline void read(T1 &);
template <class T1> inline void write(T1);
int a[N];
int main() {
  int n, k;
  read(n);
  read(k);
  for (int i = 1; i <= n; ++i) {
    read(a[i]);
  }
  sort(a + 1, a + n + 1);
  int ans = inf;
  for (int i = 1; i <= n - k + 1; ++i) {
    ans = min(ans, a[i + k - 1] - a[i]);
  }
  write(ans);
  EL;
  return 0;
}
template <class T1> void read(T1 &r_e_a_d) {
  T1 k = 0;
  char ch = getchar();
  int flag = 1;
  while (!isNum(ch)) {
    if (ch == '-') {
      flag = -1;
    }
    ch = getchar();
  }
  while (isNum(ch)) {
    k = (k << 1) + (k << 3) + ch - '0';
    ch = getchar();
  }
  r_e_a_d = flag * k;
}
template <class T1> void write(T1 w_r_i_t_e) {
  if (w_r_i_t_e < 0) {
    putchar('-');
    write(-w_r_i_t_e);
  } else {
    if (w_r_i_t_e < 10) {
      putchar(w_r_i_t_e + '0');
    } else {
      write(w_r_i_t_e / 10);
      putchar((w_r_i_t_e % 10) + '0');
    }
  }
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
using namespace std;
typedef long long ll;
const int mod = 1000000007;
const ll mod2 = 998244353;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> h(n);
  rep(i, n) cin >> h[i];
  sort(h.begin(), h.end());
  int ans = mod;
  rep(i, n + 1 - k) {
    int cans = h[k - 1 - i] - h[i];
    ans = min(ans, cans);
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repr(i, n) for (int i = (n); i >= 0; --i)
#define FOR(i, m, n) for (int i = (m); i < (n); ++i)
#define FORR(i, m, n) for (int i = (m); i >= (n); --i)
using namespace std;
typedef long long ll;
const int mod = 1000000007;
const ll mod2 = 998244353;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> h(n);
  rep(i, n) cin >> h[i];
  sort(h.begin(), h.end());
  int ans = mod;
  rep(i, n + 1 - k) {
    int cans = h[k - 1 + i] - h[i];
    ans = min(ans, cans);
  }
  cout << ans << endl;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  long long int tc, k, a[100];
  while (cin >> tc >> k) {
    for (int i = 0; i < tc; i++) {
      cin >> a[i];
    }
    sort(a, a + tc);
    long long ma = 9e18;
    for (int i = 0; i <= tc - k; i++) {
      // cout<<a[i+k-1]<<' '<<a[i]<<endl;
      ma = min(ma, a[i + k - 1] - a[i]);
    }
    cout << ma << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int tc, k, a[100005];
  while (cin >> tc >> k) {
    for (int i = 0; i < tc; i++) {
      cin >> a[i];
    }
    sort(a, a + tc);
    long long ma = 9e18;
    for (int i = 0; i <= tc - k; i++) {
      // cout<<a[i+k-1]<<' '<<a[i]<<endl;
      ma = min(ma, a[i + k - 1] - a[i]);
    }
    cout << ma << endl;
  }
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <queue>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

int main() {
  uint64_t N, K;
  cin >> N >> K;

  uint64_t tree[101000];
  for (int i = 0; i < N; ++i) {
    cin >> tree[i];
  }
  sort(tree, &tree[N], std::greater<>());
  uint64_t difMin = tree[0] - tree[K - 1];
  for (int i = 0; i < N - K; ++i) {
    difMin = min(difMin, tree[i] - tree[i - K - 1]);
  }
  cout << difMin << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdint>
#include <iostream>
#include <queue>
#include <string.h>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

int main() {
  uint64_t N, K;
  cin >> N >> K;

  uint64_t tree[101000];
  for (int i = 0; i < N; ++i) {
    cin >> tree[i];
  }
  sort(tree, &tree[N], std::greater<>());
  uint64_t difMin = tree[0] - tree[K - 1];
  for (int i = 0; i < N - K + 1; ++i) {
    difMin = min(difMin, tree[i] - tree[i + K - 1]);
  }
  cout << difMin << endl;
  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int arr[N], L[N], R[N];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  sort(arr, arr + n);
  int ans = 1e17;
  for (int i = 0; i < n; i++) {
    ans = min(ans, arr[i + k - 1] - arr[i]);
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 5;
int arr[N], L[N], R[N];
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, k;
  cin >> n >> k;
  int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  sort(arr, arr + n);
  int ans = 1e17;
  for (int i = 0; i <= n - k; i++) {
    ans = min(ans, arr[i + k - 1] - arr[i]);
  }
  cout << ans;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;
long long a[1000005];

int main() {
  long long n, k;
  long long i;
  cin >> n >> k;
  for (i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  long long min = 1000;
  for (i = 2; i < n; i++) {
    if (a[i] - a[i - k + 1] < min)
      min = a[i] - a[i - k + 1];
  }
  cout << min << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
long long a[1000005];

int main() {
  long long n, k;
  long long i;
  cin >> n >> k;
  for (i = 0; i < n; i++)
    cin >> a[i];
  sort(a, a + n);
  long long min = a[k - 1] - a[0];
  for (i = k - 1; i < n; i++) {
    if (a[i] - a[i - k + 1] < min)
      min = a[i] - a[i - k + 1];
  }
  cout << min << endl;
  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n, k;
  int h[10000];
  int min = 100000000;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }

  sort(h, h + n);

  for (int i = 0; i <= n - k; i++) {
    if (h[i + k - 1] - h[i] < min) {
      min = h[i + k - 1] - h[i];
    }
  }
  cout << min << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;

int main() {
  int n, k;
  int h[100000];
  int min = 1000000000;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }

  sort(h, h + n);

  for (int i = 0; i <= n - k; i++) {
    if (h[i + k - 1] - h[i] < min) {
      min = h[i + k - 1] - h[i];
    }
  }
  cout << min << endl;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(n) begin(n), end(n)

int main() {
  int n, k;
  cin >> n >> k;

  vector<int> h(n);
  REP(i, n) { cin >> h[i]; }
  sort(ALL(h));

  int d, d_min;
  REP(i, n - 2) {
    d = h[i + k - 1] - h[i];
    if (i == 0) {
      d_min = d;
    } else {
      if (d < d_min) {
        d_min = d;
      }
    }
  }

  cout << d_min << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(n) begin(n), end(n)

int main() {
  int n, k;
  cin >> n >> k;

  vector<int> h(n);
  REP(i, n) { cin >> h[i]; }
  sort(ALL(h));

  int d, d_min;
  REP(i, n - k + 1) {
    d = h[i + k - 1] - h[i];
    if (i == 0) {
      d_min = d;
    } else {
      if (d < d_min) {
        d_min = d;
      }
    }
  }

  cout << d_min << endl;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int main(int argc, char const *argv[]) {
  long long int n, k = 0;
  cin >> n >> k;
  vector<unsigned long long> h(n);

  for (unsigned long long i = 0; i < n; i++) {
    cin >> h[i];
  }

  sort(h.begin(), h.end());

  unsigned long long min = 0 - 1;

  for (unsigned long long i = n - 1; i >= n - k; i--) {
    if (min > h[i] - h[i - k + 1]) {
      min = h[i] - h[i - k + 1];
    }
  }

  cout << min << endl;
}


Fixed submission:
#include <algorithm>
#include <array>
#include <cstdlib>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

int main(int argc, char const *argv[]) {
  long long int n, k = 0;
  cin >> n >> k;
  vector<unsigned long long> h(n);

  for (unsigned long long i = 0; i < n; i++) {
    cin >> h[i];
  }

  sort(h.begin(), h.end());

  unsigned long long min = 0 - 1;

  for (unsigned long long i = n - 1; i >= k - 1; i--) {
    if (min > h[i] - h[i - k + 1]) {
      min = h[i] - h[i - k + 1];
    }
  }

  cout << min << endl;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define MOD 1000000007
#define INF (1e9)
#define PI (acos(-1))
#define print(x) cout << x << endl
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> h(N);
  rep(i, N) cin >> h[i];

  sort(h.begin(), h.end());
  ll ans = INF;
  rep(i, K) { ans = min(ans, h[i + K - 1] - h[i]); }

  print(ans);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define MOD 1000000007
#define INF (1e9)
#define PI (acos(-1))
#define print(x) cout << x << endl
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl

int main() {
  int N, K;
  cin >> N >> K;
  vector<ll> h(N);
  rep(i, N) cin >> h[i];

  sort(h.begin(), h.end());
  ll ans = INF;
  rep(i, N - K + 1) { ans = min(ans, h[i + K - 1] - h[i]); }

  print(ans);
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> h(N);
  for (int i = 0; i < N; ++i)
    cin >> h[i];

  sort(h.begin(), h.end());

  int ans = 1e9;
  for (int i = 0; i < N; i += K - 1) {
    ans = min(ans, h[i + K - 1] - h[i]);
  }

  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> h(N);
  for (int i = 0; i < N; ++i)
    cin >> h[i];

  sort(h.begin(), h.end());

  int ans = 1e9;
  for (int i = 0; i < N - K + 1; i++) {
    ans = min(ans, h[i + K - 1] - h[i]);
  }

  cout << ans;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

vector<int> h(100010);

int main() {
  int N, K;
  cin >> N >> K;
  for (int i = 0; i < N; ++i) {
    cin >> h[i];
  }

  sort(h.begin(), h.begin() + N);

  int ans = 1e9;
  for (int i = 0; i < K; ++i) {
    ans = min(ans, h[i + K - 1] - h[i]);
  }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

vector<int> h(100010);

int main() {
  int N, K;
  cin >> N >> K;
  for (int i = 0; i < N; ++i) {
    cin >> h[i];
  }

  sort(h.begin(), h.begin() + N);

  int ans = 1e9;
  for (int i = 0; i <= N - K; ++i) {
    ans = min(ans, h[i + K - 1] - h[i]);
  }

  cout << ans << endl;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int a[101];
int main() {
  int n, i, k, minn = INT_MAX;
  scanf("%d %d", &n, &k);
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + 1 + n);
  for (i = 1; i <= n - k + 1; i++)
    minn = min(minn, a[i + k - 1] - a[i]);
  printf("%d", minn);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int a[100001];
int main() {
  int n, i, k, minn = INT_MAX;
  scanf("%d %d", &n, &k);
  for (i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  sort(a + 1, a + 1 + n);
  for (i = 1; i <= n - k + 1; i++)
    minn = min(minn, a[i + k - 1] - a[i]);
  printf("%d", minn);
  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int INF = 1e9;
const long long LINF = 1e18;

int main() {
  int N, K;
  vector<int> h(N);
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  sort(h.begin(), h.end());
  int ans = INF;
  int now;
  for (int i = 0; i < N - K + 1; i++) {
    now = h[i + K - 1] - h[i];
    ans = min(ans, now);
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
const int INF = 1e9;
const long long LINF = 1e18;

int main() {
  int N, K;
  cin >> N >> K;
  vector<int> h(N);
  for (int i = 0; i < N; i++) {
    cin >> h[i];
  }
  sort(h.begin(), h.end());
  int ans = INF;
  int now;
  for (int i = 0; i < N - K + 1; i++) {
    now = h[i + K - 1] - h[i];
    ans = min(ans, now);
  }
  cout << ans << endl;
}