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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
using namespace std;
// #define int long long
#define rep(i, s, n) for (int i = s; i < n; i++)
#define repe(i, s, n) for (int i = s; i <= n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define fi first
#define se second
#define chmin(a, b) a = min((a), (b))
#define chmax(a, b) a = max((a), (b))
typedef long long ll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
typedef pair<pint, int> P1;
typedef pair<int, pint> P2;
typedef pair<pint, pint> PP;
static const ll maxLL = (ll)1 << 62;
const ll MOD = 1000000007;
const ll INF = 1e18;
const double PI = 3.14159265;
int ca[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

signed main() {
  int i, j;
  int n, k;
  vector<int> list;

  cin >> n >> k;

  for (i = 0; i < n; i++) {
    int num;
    cin >> num;
    list.push_back(num);
  }

  sort(list.begin(), list.end(), greater<int>());

  int minn = 999999999;

  for (i = 0; i < n + (k - 1); i++) {
    minn = min(minn, list[i] - list[i + (k - 1)]);
  }
  cout << minn << endl;

  getchar();
  getchar();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
using namespace std;
// #define int long long
#define rep(i, s, n) for (int i = s; i < n; i++)
#define repe(i, s, n) for (int i = s; i <= n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define fi first
#define se second
#define chmin(a, b) a = min((a), (b))
#define chmax(a, b) a = max((a), (b))
typedef long long ll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
typedef pair<pint, int> P1;
typedef pair<int, pint> P2;
typedef pair<pint, pint> PP;
static const ll maxLL = (ll)1 << 62;
const ll MOD = 1000000007;
const ll INF = 1e18;
const double PI = 3.14159265;
int ca[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

signed main() {
  int i, j;
  int n, k;
  vector<int> list;

  cin >> n >> k;

  for (i = 0; i < n; i++) {
    int num;
    cin >> num;
    list.push_back(num);
  }

  sort(list.begin(), list.end(), greater<int>());

  int minn = 999999999;

  for (i = 0; i < n - (k - 1); i++) {
    minn = min(minn, list[i] - list[i + (k - 1)]);
  }
  cout << minn << endl;

  getchar();
  getchar();
  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
using namespace std;
// #define int long long
#define rep(i, s, n) for (int i = s; i < n; i++)
#define repe(i, s, n) for (int i = s; i <= n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define fi first
#define se second
#define chmin(a, b) a = min((a), (b))
#define chmax(a, b) a = max((a), (b))
typedef long long ll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
typedef pair<pint, int> P1;
typedef pair<int, pint> P2;
typedef pair<pint, pint> PP;
static const ll maxLL = (ll)1 << 62;
const ll MOD = 1000000007;
const ll INF = 1e18;
const double PI = 3.14159265;
int ca[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

signed main() {
  int i, j;
  int n, k;
  vector<int> list;

  cin >> n >> k;

  for (i = 0; i < n; i++) {
    int num;
    cin >> num;
    list.push_back(num);
  }

  sort(list.begin(), list.end(), greater<int>());

  int minn = 999999999;

  for (i = 0; i < n - (k - 1); i++) {
    minn = min(minn, list[i] - list[i - (k - 1)]);
  }
  cout << minn << endl;

  getchar();
  getchar();
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cstdlib>
#include <ctype.h>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <tuple>
#include <vector>
using namespace std;
// #define int long long
#define rep(i, s, n) for (int i = s; i < n; i++)
#define repe(i, s, n) for (int i = s; i <= n; i++)
#define rrep(i, s, n) for (int i = (n)-1; i >= (s); i--)
#define all(v) (v).begin(), (v).end()
#define pb push_back
#define fi first
#define se second
#define chmin(a, b) a = min((a), (b))
#define chmax(a, b) a = max((a), (b))
typedef long long ll;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
typedef pair<pint, int> P1;
typedef pair<int, pint> P2;
typedef pair<pint, pint> PP;
static const ll maxLL = (ll)1 << 62;
const ll MOD = 1000000007;
const ll INF = 1e18;
const double PI = 3.14159265;
int ca[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

signed main() {
  int i, j;
  int n, k;
  vector<int> list;

  cin >> n >> k;

  for (i = 0; i < n; i++) {
    int num;
    cin >> num;
    list.push_back(num);
  }

  sort(list.begin(), list.end(), greater<int>());

  int minn = 999999999;

  for (i = 0; i < n - (k - 1); i++) {
    minn = min(minn, list[i] - list[i + (k - 1)]);
  }
  cout << minn << endl;

  getchar();
  getchar();
  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  const int M = 1e9;
  int n, k, h[M], ans = 1e9;
  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++)
    ans = min(ans, h[i + k - 1] - h[i]);
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  const int M = 1e5;
  int n, k, h[M], ans = 1e9;
  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++)
    ans = min(ans, h[i + k - 1] - h[i]);
  cout << ans << endl;
}
Problem p03208 (C++)

Buggy submission (status: Time Limit Exceeded):
#ifdef DEBUG

#else

#pragma GCC optimize("O3,no-stack-protector")
#pragma GCC optimize("unroll-loops")

#if __cplusplus >= 201703L
#pragma GCC target("avx2")
#else
#pragma GCC target("avx")
#endif

#endif
// 実数の場合
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define dump(x) cout << x << endl
typedef int64_t Int;

using namespace std;
using Graph = vector<vector<Int>>;

const double pi = M_PI;
const Int MOD = 1000000007;

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

  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 ro = n - k;
  int ans = INT32_MAX;
  for (int i = 0; i <= ro; i++) {
    ans =
        min(ans, *max_element(next(h.begin(), i), next(h.begin(), i + k)) -
                     *min_element(next(h.begin(), i), next(h.begin(), i + k)));
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#ifdef DEBUG

#else

#pragma GCC optimize("O3,no-stack-protector")
#pragma GCC optimize("unroll-loops")

#if __cplusplus >= 201703L
#pragma GCC target("avx2")
#else
#pragma GCC target("avx")
#endif

#endif
// 実数の場合
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#define dump(x) cout << x << endl
typedef int64_t Int;

using namespace std;
using Graph = vector<vector<Int>>;

const double pi = M_PI;
const Int MOD = 1000000007;

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

  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 ro = n - k;
  int ans = INT32_MAX;
  for (int i = 0; i <= ro; i++) {
    ans = min(ans, h[i + k - 1] - h[i]);
  }

  cout << ans << endl;

  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iomanip>
#include <iostream>
using namespace std;
#include <cmath>
#include <cstdio>
#include <deque>
#include <map>
#include <set>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using ll = long long;
using Graph = vector<vector<int>>;
#include <queue>
#include <stack>

// 最大公約数
ll gcd(ll a, ll b) {
  if (b == 0LL)
    return a;
  return gcd(b, a % b);
}

// 最小公倍数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {

  int n;
  int k;
  cin >> n >> k;
  vector<ll> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }

  sort(h.begin(), h.end());
  ll min = 100000000000;
  for (int i = 0; i < n; i++) {

    ll t = abs(h[i] - h[i + k - 1]);
    if (min > t) {
      min = t;
    }
  }

  cout << min << endl;
}

Fixed submission:
#include <algorithm>
#include <iomanip>
#include <iostream>
using namespace std;
#include <cmath>
#include <cstdio>
#include <deque>
#include <map>
#include <set>
#include <string.h>
#include <string>
#include <tuple>
#include <vector>
using ll = long long;
using Graph = vector<vector<int>>;
#include <queue>
#include <stack>

// 最大公約数
ll gcd(ll a, ll b) {
  if (b == 0LL)
    return a;
  return gcd(b, a % b);
}

// 最小公倍数
ll lcm(ll a, ll b) { return a / gcd(a, b) * b; }

int main() {

  int n;
  int k;
  cin >> n >> k;
  vector<ll> h(n);
  for (int i = 0; i < n; i++) {
    cin >> h[i];
  }

  sort(h.begin(), h.end());
  ll min = 100000000000;
  for (int i = 0; i + k - 1 < n; i++) {

    ll t = abs(h[i] - h[i + k - 1]);
    if (min > t) {
      min = t;
    }
  }

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

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
#define ll long long

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

  sort(h, h + n);
  ll minSumH = 1000000000;

  for (int i = 0; i < n - k + 1; ++i) {
    ll tmp = 0;

    for (int j = 0; j < k - 1; ++j) {
      tmp += h[i + j + 1] - h[i + j];
    }

    minSumH = min(minSumH, tmp);
    if (minSumH == 0) {
      break;
    }
  }

  cout << minSumH << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
#define ll long long

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

  sort(h, h + n);
  ll minSumH = 1000000000;

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

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

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

using namespace std;

int main() {
  int K, N;
  cin >> N >> K;
  vector<int> heights(N);
  for (int i = 0; i < N; ++i) {
    cin >> heights[i];
  }
  sort(heights.begin(), heights.end());
  int minDiff = 2000000000;
  for (int i = 0; i < K; ++i) {
    int diff = heights[i + K - 1] - heights[i];
    if (diff < minDiff) {
      minDiff = diff;
    }
  }
  cout << minDiff << endl;
}

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

using namespace std;

int main() {
  int K, N;
  cin >> N >> K;
  vector<int> heights(N);
  for (int i = 0; i < N; ++i) {
    cin >> heights[i];
  }
  sort(heights.begin(), heights.end());
  int minDiff = 2000000000;
  for (int i = 0; i <= N - K; ++i) {
    int diff = heights[i + K - 1] - heights[i];
    if (diff < minDiff) {
      minDiff = diff;
    }
  }
  cout << minDiff << endl;
}
Problem p03208 (C++)

Buggy submission (status: Time Limit Exceeded):
// median min 25mins
// time
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
#define all(v) v.begin(), v.end()
#define repi(i, n, init) for (ll i = init; i < (n); i++)
#define repd(i, n, init) for (ll i = (n); i >= init; i--)
#define repm(i, m) for (auto i = m.begin(); i != m.end(); i++)

int main() {
  ll N, K, m, M, ans = INF;
  cin >> N >> K;
  vector<ll> H(N);
  repi(i, N, 0) { cin >> H[i]; }
  sort(all(H));
  repi(i, N - K + 1, 0) {
    M = 0;
    m = INF;
    repi(j, i + K, i) {
      M = max(M, H.at(j));
      m = min(m, H.at(j));
    }
    ans = min(ans, M - m);
    if (ans == 0) {
      cout << ans << endl;
      return 0;
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
// median min 25mins
// time
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e18;
const ll MOD = 1e9 + 7;
#define all(v) v.begin(), v.end()
#define repi(i, n, init) for (ll i = init; i < (n); i++)
#define repd(i, n, init) for (ll i = (n); i >= init; i--)
#define repm(i, m) for (auto i = m.begin(); i != m.end(); i++)

int main() {
  ll N, K, m, M, ans = INF;
  cin >> N >> K;
  vector<ll> H(N);
  repi(i, N, 0) { cin >> H[i]; }
  sort(all(H));
  for (ll i = 0; i < N - K + 1; i++) {
    ans = min(ans, H.at(i + K - 1) - H.at(i));
  }
  cout << ans << endl;
  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)

int main() {
  int N, K;
  cin >> N >> K;
  //  int64_t hmax = 0, hmin = 1e10;
  vector<int64_t> H(N), A(K);
  rep(i, N) { cin >> H[i]; }
  sort(H.begin(), H.end());
  int64_t ans = 1e10;
  //    int A[K];
  for (int64_t i = 0; i < N - K + 1; i++) {
    int64_t htmp1 = 0, htmp2 = 1e10;
    for (int64_t j = i; j < K + i; j++) {
      //        cout << "i=" << i << ",j=" << j << endl;
      //          if(j==0){
      A[j - i] = H[j];
      //        }
      //      else{}
      //          cout << htmp1 << " " << htmp2 << endl;
    }
    //      cout << htmp1 << " " << htmp2 << endl;
    sort(A.begin(), A.end());
    ans = min(ans, A[K - 1] - A[0]);
    if (ans == 0)
      break;
  }

  cout << ans << endl;
}

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

int main() {
  int N, K;
  cin >> N >> K;
  //  int64_t hmax = 0, hmin = 1e10;
  vector<int64_t> H(N), A(K);
  rep(i, N) { cin >> H[i]; }
  sort(H.begin(), H.end());
  int64_t ans = 1e10;
  //    int A[K];
  for (int64_t i = 0; i < N - K + 1; i++) {
    int64_t htmp1 = 0, htmp2 = 1e10;
    for (int64_t j = i; j < K + i; j++) {
      //        cout << "i=" << i << ",j=" << j << endl;
      //          if(j==0){
      A[j - i] = H[j];
      //        }
      //      else{}
      //          cout << htmp1 << " " << htmp2 << endl;
    }
    //      cout << htmp1 << " " << htmp2 << endl;
    //        sort(A.begin(), A.end());
    ans = min(ans, A[K - 1] - A[0]);
    if (ans == 0)
      break;
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
typedef long long ll;
using namespace std;
ll intpow(int n, int m) {
  if (m == 0)
    return 1;
  return n * intpow(n, m - 1);
}
ll gcd(ll n, ll m) {
  n = max(n, m);
  m = min(n, m);
  if (n % m == 0)
    return m;
  return gcd(m, n % m);
}
ll lcm(ll n, ll m) { return n * m / gcd(n, m); }
int main() {
  int k, n;
  cin >> n >> k;
  vector<int> h, s;
  h.resize(n);
  s.resize(n - k);
  for (int i = 0; i < n; i++)
    cin >> h[i];
  sort(h.begin(), h.end());
  for (int i = 0; i < n - k + 1; i++)
    s[i] = h[i + k - 1] - h[i];
  sort(s.begin(), s.end());
  cout << s[0] << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
typedef long long ll;
using namespace std;
ll intpow(int n, int m) {
  if (m == 0)
    return 1;
  return n * intpow(n, m - 1);
}
ll gcd(ll n, ll m) {
  n = max(n, m);
  m = min(n, m);
  if (n % m == 0)
    return m;
  return gcd(m, n % m);
}
ll lcm(ll n, ll m) { return n * m / gcd(n, m); }
int main() {
  int k, n;
  cin >> n >> k;
  vector<int> h, s;
  h.resize(n);
  s.resize(n - k + 1);
  for (int i = 0; i < n; i++)
    cin >> h[i];
  sort(h.begin(), h.end());
  for (int i = 0; i < n - k + 1; i++)
    s[i] = h[i + k - 1] - h[i];
  sort(s.begin(), s.end());
  cout << s[0] << endl;
}
Problem p03208 (C++)

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

using namespace std;

int main(void) {
  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 = 1000000007;
  for (int i = N - 1; i >= N - K; i--) {
    long long dist;
    dist = h[i] - h[i + 1 - K];
    ans = min(ans, dist);
  }
  cout << ans << endl;
  return 0;
}


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

using namespace std;

int main(void) {
  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 = 1000000007;
  for (int i = 0; i <= N - K; i++) {
    ans = min(ans, h[i + K - 1] - h[i]);
  }
  cout << ans << 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];
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> h[i];
  sort(h, h + N);

  int max = h[K - 1], min = h[0];
  int result = max - min;
  for (int i = 1; i <= N - K; i++) {
    max = h[i + K - 1];
    min = h[i];
    if (result > (max - min)) {
      result = max - min;
    }
  }
  cout << result << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
  int N, K;
  int h[100000];
  cin >> N >> K;
  for (int i = 0; i < N; i++)
    cin >> h[i];
  sort(h, h + N);

  int max = h[K - 1], min = h[0];
  int result = max - min;
  for (int i = 1; i <= N - K; i++) {
    max = h[i + K - 1];
    min = h[i];
    if (result > (max - min)) {
      result = max - min;
    }
  }
  cout << result << endl;
  return 0;
}
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 rep2(i, n) for (int i = 1; i < (n); i++)
typedef long long ll;

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 = h.at(n);

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

  cout << ans << endl;
}

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

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 = h.at(n - 1);

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

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

Buggy submission (status: Runtime Error):
// abc115_c.cpp
// Sat Dec 15 19:18:11 2018

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>
#define INTINF 2147483647
#define LLINF 9223372036854775807
using namespace std;
using ll = long long;
typedef pair<int, int> P;

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

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

  sort(h, h + n);

  int ans = INTINF;
  int prevhight = 0;
  for (int i = 0; i < n; i++) {
    if (prevhight == h[i]) {
      continue;
    }
    if (ans > h[i + k - 1] - h[i]) {
      ans = h[i + k - 1] - h[i];
    }
  }

  cout << ans << endl;
}

Fixed submission:
// abc115_c.cpp
// Sat Dec 15 19:18:11 2018

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>
#define INTINF 2147483647
#define LLINF 9223372036854775807
using namespace std;
using ll = long long;
typedef pair<int, int> P;

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

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

  sort(h, h + n);

  int ans = INTINF;
  int prevhight = 0;
  for (int i = 0; i < n - k + 1; i++) {
    if (prevhight == h[i]) {
      continue;
    }
    if (ans > h[i + k - 1] - h[i]) {
      ans = h[i + k - 1] - h[i];
    }
  }

  cout << ans << endl;
}
Problem p03208 (Python)

Buggy submission (status: Time Limit Exceeded):
def main():
    N, K = list(map(int, input().split()))
    hs = [int(input()) for i in range(N)]
    hs.sort()
    i = 0
    r = 10**9
    while True:
        if i + K > N:
            break
        th = hs[i : i + K]
        r = min(r, th[-1] - th[0])
        i += 1
    print(r)


if __name__ == "__main__":
    main()


Fixed submission:
def main():
    N, K = list(map(int, input().split()))
    hs = [int(input()) for i in range(N)]
    hs.sort()
    i = 0
    r = 10**9
    while True:
        if i + K > N:
            break
        r = min(r, hs[i + K - 1] - hs[i])
        i += 1
    print(r)


if __name__ == "__main__":
    main()
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
//{{{
#include <bits/stdc++.h>
using namespace std;
#define repX(a, b, c, x, ...) x
#define repN(a) repX a
#define rep(...) repN((__VA_ARGS__, rep3, rep2, loop))(__VA_ARGS__)
#define rrep(...) repN((__VA_ARGS__, rrep3, rrep2))(__VA_ARGS__)
#define loop(n) rep2(i_, n)
#define rep2(i, n) rep3(i, 0, n)
#define rep3(i, begin, end)                                                    \
  for (int i = (int)(begin), i##_end = (int)(end); i < i##_end; ++i)
#define rrep2(i, n) rrep3(i, n, 0)
#define rrep3(i, begin, end)                                                   \
  for (int i = (int)(begin - 1), i##_end = (int)(end); i >= i##_end; --i)
#define foreach(x, a) for (auto &x : a)
using ll = long long;
const ll mod = (ll)1e9 + 7;
//}}}

int p[10000];
int main() {
  int N, K;
  cin >> N >> K;

  rep(i, N) { cin >> p[i]; }
  sort(p, p + N);

  int ans = p[N - 1] - p[0];
  rep(i, N - K + 1) { ans = min(ans, p[i + K - 1] - p[i]); }
  cout << ans << endl;
  return 0;
}


Fixed submission:
//{{{
#include <bits/stdc++.h>
using namespace std;
#define repX(a, b, c, x, ...) x
#define repN(a) repX a
#define rep(...) repN((__VA_ARGS__, rep3, rep2, loop))(__VA_ARGS__)
#define rrep(...) repN((__VA_ARGS__, rrep3, rrep2))(__VA_ARGS__)
#define loop(n) rep2(i_, n)
#define rep2(i, n) rep3(i, 0, n)
#define rep3(i, begin, end)                                                    \
  for (int i = (int)(begin), i##_end = (int)(end); i < i##_end; ++i)
#define rrep2(i, n) rrep3(i, n, 0)
#define rrep3(i, begin, end)                                                   \
  for (int i = (int)(begin - 1), i##_end = (int)(end); i >= i##_end; --i)
#define foreach(x, a) for (auto &x : a)
using ll = long long;
const ll mod = (ll)1e9 + 7;
//}}}

int p[100000];
int main() {
  int N, K;
  cin >> N >> K;

  rep(i, N) { cin >> p[i]; }
  sort(p, p + N);

  int ans = p[N - 1] - p[0];
  rep(i, N - K + 1) { ans = min(ans, p[i + K - 1] - p[i]); }
  cout << ans << endl;
  return 0;
}
Problem p03208 (C++)

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

using namespace std;

long long MIN(long long a, long long b) {
  if (a < b)
    return a;
  return b;
}

int main() {

  long long n, k;
  cin >> n >> k;
  long long h[n];

  for (int i = 0; i < n; i++)
    cin >> h[i];

  sort(h, h + n);

  long long tmp = 1000000000;
  for (int i = 0; i < (n - 2); i++) {
    tmp = MIN(h[i + k - 1] - h[i], tmp);
  }

  cout << tmp << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <iostream>
#include <string>

using namespace std;

long long MIN(long long a, long long b) {
  if (a < b)
    return a;
  return b;
}

int main() {

  long long n, k;
  cin >> n >> k;
  long long h[n];

  for (int i = 0; i < n; i++)
    cin >> h[i];

  sort(h, h + n);

  long long tmp = 1000000000;
  for (int i = 0; i < (n - k + 1); i++) {
    tmp = MIN(h[i + k - 1] - h[i], tmp);
  }

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

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
typedef long long ll;
using namespace std;
ll a[10005];
int main() {
  long long m, n;
  cin >> m >> n;
  for (int i = 0; i < m; i++) {
    cin >> a[i];
  }
  sort(a, a + m);
  long long ans = a[m - 1];
  for (int i = 0; i + n - 1 < m; i++) {
    ans = min(ans, a[i + n - 1] - a[i]);
  }
  printf("%lld", ans);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
typedef long long ll;
using namespace std;
ll a[100005];
int main() {
  long long m, n;
  cin >> m >> n;
  for (int i = 0; i < m; i++) {
    cin >> a[i];
  }
  sort(a, a + m);
  long long ans = a[m - 1];
  for (int i = 0; i + n - 1 < m; i++) {
    ans = min(ans, a[i + n - 1] - a[i]);
  }
  printf("%lld", ans);
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
#define ms(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
ll h[10005];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &h[i]);
  }
  sort(h + 1, h + 1 + n);
  ll ans = inf;
  for (int i = 1; (i + k - 1) <= n; i++) {
    ans = min(ans, (h[k + i - 1] - h[i]));
  }
  printf("%lld\n", ans);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
#define pi acos(-1.0)
#define ms(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
ll h[100005];
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= n; i++) {
    scanf("%lld", &h[i]);
  }
  sort(h + 1, h + 1 + n);
  ll ans = inf;
  for (int i = 1; (i + k - 1) <= n; i++) {
    ans = min(ans, (h[k + i - 1] - h[i]));
  }
  printf("%lld\n", ans);
  return 0;
}
Problem p03208 (Python)

Buggy submission (status: Runtime Error):
import numpy as np

n, k = map(int, input().split())
a = [int(input()) for x in range(n)]
a.sort()
a = np.array(a)
print(np.min(a[k - 1 :] - a[:k]))


Fixed submission:
import numpy as np

n, k = map(int, input().split())
a = [int(input()) for x in range(n)]
a.sort()
a = np.array(a)
print(np.min(a[k - 1 :] - a[: n - k + 1]))
Problem p03208 (Python)

Buggy submission (status: Runtime Error):
def main(n: int, k: int, h: list):
    h.sort()

    print(min([h[i + k - 1] - h[i] for i in range(n - k + 1)]))


if __name__ == "__main__":
    n, k = map(int, input().split())
    h = [input() for _ in range(n)]

    main(n, k, h)


Fixed submission:
def main(n: int, k: int, h: list):
    h.sort()

    print(min([h[i + k - 1] - h[i] for i in range(n - k + 1)]))


if __name__ == "__main__":
    n, k = map(int, input().split())
    h = [int(input()) for _ in range(n)]

    main(n, k, h)
Problem p03208 (Python)

Buggy submission (status: Runtime Error):
N, K = list(map(int, input().split()))
h = []
for i in range(N):
    h.append(int(input()))
else:
    h.sort()

hi = []
i = 0
while i + (K - 1) < N:
    hi.append(int(h[i + 2] - h[i]))
    i += 1

print(min(hi))


Fixed submission:
N, K = list(map(int, input().split()))
h = []
for i in range(N):
    h.append(int(input()))
else:
    h.sort()

hi = []
i = 0
while i + (K - 1) < N:
    hi.append(int(h[i + (K - 1)] - h[i]))
    i += 1

print(min(hi))
Problem p03208 (C++)

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

using namespace std;

int main(int argc, char *argv[]) {
  int N, K;
  cin >> N >> K;

  vector<long long> trees(N);
  for (int i = 0; i < N; ++i)
    cin >> trees[i];

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

  long long ret = 1000000000;
  for (int i = 0; i < N; ++i) {
    ret = min(ret, trees[i + K] - trees[i]);
  }

  cout << ret << endl;

  return 0;
}

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

using namespace std;

int main(int argc, char *argv[]) {
  int N, K;
  cin >> N >> K;

  vector<long long> trees(N);
  for (int i = 0; i < N; ++i)
    cin >> trees[i];

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

  long long ret = 1000000000;
  for (int i = 0; i <= N - K; ++i) {
    ret = min(ret, trees[i + K - 1] - trees[i]);
  }

  cout << ret << endl;

  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <iostream>
#include <math.h>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> h(n);

  for (int i = 0; i < n; i++) {
    cin >> h.at(i);
  }
  sort(h.begin(), h.end());
  long long min_diff = LONG_MAX;
  for (int i = 0; i < n - k + 1; i++) {
    int max_height = 0;
    int min_height = INT_MAX;
    for (int j = 0; j < k; j++) {
      if (max_height < h.at(i + j))
        max_height = h.at(i + j);
      if (min_height > h.at(i + j))
        min_height = h.at(i + j);
    }
    if (max_height - min_height < min_diff)
      min_diff = max_height - min_height;
  }
  cout << min_diff << endl;
  ;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <iostream>
#include <math.h>
#include <vector>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  vector<int> h(n);

  for (int i = 0; i < n; i++) {
    cin >> h.at(i);
  }
  sort(h.begin(), h.end());
  long long min_diff = LONG_MAX;
  for (int i = 0; i < n - k + 1; i++) {
    int max_height = h.at(i + k - 1);
    int min_height = h.at(i);
    if (max_height - min_height < min_diff)
      min_diff = max_height - min_height;
  }
  cout << min_diff << endl;
  ;
}
Problem p03208 (C++)

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

int main() {
  int N, K, dif, min = 2000000000;
  cin >> N >> K;
  int h[100000];
  for (int i = 0; i < N; i++)
    cin >> h[i];
  // 降順ソート
  sort(h, h + N, greater<int>());
  // 差
  for (int i = 0; i < N; i++) {
    dif = h[i] - h[i + K - 1];
    if (dif < min)
      min = dif;
  }
  cout << min << endl;
  return 0;
}

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

int main() {
  int N, K, dif, min = 2000000000;
  cin >> N >> K;
  int h[100000];
  for (int i = 0; i < N; i++)
    cin >> h[i];
  // 降順ソート
  sort(h, h + N, greater<int>());
  // 差
  for (int i = 0; i + K - 1 < N; i++) {
    dif = h[i] - h[i + K - 1];
    if (dif < min)
      min = dif;
  }
  cout << min << endl;
  return 0;
}
Problem p03208 (C++)

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

#define OnlineJudge1

#define loop(a, n) for (int i = a; i < n; i++)
#define NumofDigits(n) ((int)log10(n) + 1)
#define CountofNumber(array, n, x)                                             \
  (upper_bound(array, array + n, x) - lower_bound(array, array + n, x))
#define readline(s) getline(cin, s)
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define EPS 1e-7
#define modulo ll(1e9 + 7)

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;

void init() {
  cin.tie(0);
  cin.sync_with_stdio(0);
#ifdef OnlineJudge
  freopen("in.in", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
}
int main() {
  init();
  ll n, k;
  cin >> n >> k;
  int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  sort(arr, arr + n);
  int ans = INT_MAX;
  for (int i = 0; i < n; i += k) {
    ans = min(ans, arr[i + k - 1] - arr[i]);
  }
  cout << ans;
}

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

#define OnlineJudge1

#define loop(a, n) for (int i = a; i < n; i++)
#define NumofDigits(n) ((int)log10(n) + 1)
#define CountofNumber(array, n, x)                                             \
  (upper_bound(array, array + n, x) - lower_bound(array, array + n, x))
#define readline(s) getline(cin, s)
#define PB push_back
#define MP make_pair
#define F first
#define S second
#define EPS 1e-7
#define modulo ll(1e9 + 7)

typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pi;

void init() {
  cin.tie(0);
  cin.sync_with_stdio(0);
#ifdef OnlineJudge
  freopen("in.in", "r", stdin);
  freopen("out.txt", "w", stdout);
#endif
}
int main() {
  init();
  ll n, k;
  cin >> n >> k;
  int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  sort(arr, arr + n);
  int ans = INT_MAX;
  for (int i = 0; i + k - 1 < n; i++) {
    ans = min(ans, arr[i + k - 1] - arr[i]);
  }
  cout << ans;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
// freopen("transmitters.in","r",stdin);
using namespace std;

void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
/*long long mn=1e9;
void lucky (long long a, long long c,int b)
{
    if(a > 1e11)
    return ;
    if(a>=b&&c==0)
    mn=min(mn,a);

    lucky (a*10+7,c-1,b);
    lucky (a*10+4,c+1,b);

}*/
long long toint(string s) {
  long long a = 0, c = 1;
  ;
  for (int i = 0; i < s.size(); i++) {
    a += (s[s.size() - i - 1] - '0') * c;
    // cout<<a<<endl;
    c *= 10;
  }
  return a;
}
int prime(int x) {
  int c = 0;
  if (x == 1) {
    return 0;

  } else {
    for (int j = 2; j * j <= x; j++) {
      if (x % j == 0) {
        c++;
      }
    }

    if (c == 0)
      return 1;

    return 0;
  }
}
long long arr[100000];
int main() {

  fast();

  long long n, m, c = 1e13, x = 0, y = 0, t, h, z;
  string s;
  cin >> n >> m;
  m--;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  sort(arr, arr + n);
  for (int i = 0; i < n; i++)
    c = min(c, abs(arr[i] - arr[m + i]));

  cout << c << endl;
  //	system("pause");
  // cout << "-1" << endl;
  //  transform(x.begin(), x.end(), x.begin(), ::tolower);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
// freopen("transmitters.in","r",stdin);
using namespace std;

void fast() {
  std::ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
}
/*long long mn=1e9;
void lucky (long long a, long long c,int b)
{
    if(a > 1e11)
    return ;
    if(a>=b&&c==0)
    mn=min(mn,a);

    lucky (a*10+7,c-1,b);
    lucky (a*10+4,c+1,b);

}*/
long long toint(string s) {
  long long a = 0, c = 1;
  ;
  for (int i = 0; i < s.size(); i++) {
    a += (s[s.size() - i - 1] - '0') * c;
    // cout<<a<<endl;
    c *= 10;
  }
  return a;
}
int prime(int x) {
  int c = 0;
  if (x == 1) {
    return 0;

  } else {
    for (int j = 2; j * j <= x; j++) {
      if (x % j == 0) {
        c++;
      }
    }

    if (c == 0)
      return 1;

    return 0;
  }
}
long long arr[100000];
int main() {

  fast();

  long long n, m, c = 1e13, x = 0, y = 0, t, h, z;
  string s;
  cin >> n >> m;
  m--;
  for (int i = 0; i < n; i++)
    cin >> arr[i];
  sort(arr, arr + n);
  for (int i = 0; i < n; i++)
    if ((m + i) <= (n - 1))
      c = min(c, abs(arr[i] - arr[m + i]));

  cout << c << endl;
  //	system("pause");
  // cout << "-1" << endl;
  //  transform(x.begin(), x.end(), x.begin(), ::tolower);

  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define lper(i, r, l) for (ll i = r - 1; i >= l; i--)
#define fi first
#define se second
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
const ll MOD = 1000000007;
const ll MOD9 = 998244353;
const int inf = 1e9;
const ll INF = 4e18;
const ll dy[4] = {1, 0, -1, 0};
const ll dx[4] = {0, -1, 0, 1};
int main() {
  ll N;
  ll K;
  cin >> N >> K;
  vl h(N);
  rep(i, N) { cin >> h.at(i); }
  sort(all(h));

  vl vec(N - K + 1);
  rep(i, N - 2) { vec.at(i) = h.at(i + 2) - h.at(i); }

  sort(all(vec));

  cout << vec.at(0) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define per(i, n) for (ll i = n - 1; i >= 0; i--)
#define lper(i, r, l) for (ll i = r - 1; i >= l; i--)
#define fi first
#define se second
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
using ll = long long;
using vl = vector<ll>;
using vvl = vector<vector<ll>>;
using pl = pair<ll, ll>;
const ll MOD = 1000000007;
const ll MOD9 = 998244353;
const int inf = 1e9;
const ll INF = 4e18;
const ll dy[4] = {1, 0, -1, 0};
const ll dx[4] = {0, -1, 0, 1};
int main() {
  ll N;
  ll K;
  cin >> N >> K;
  vl h(N);
  rep(i, N) { cin >> h.at(i); }
  sort(all(h));

  vl vec(N - K + 1);
  rep(i, N - K + 1) { vec.at(i) = h.at(i + K - 1) - h.at(i); }

  sort(all(vec));

  cout << vec.at(0) << endl;
}
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.at(i);
  sort(h.begin(), h.end());

  int ans = 0;
  for (int i = 0; i <= N - K; i++) {
    int dh = h.at(N - K + i) - h.at(i);
    if (i == 0)
      ans = dh;
    else if (ans > dh)
      ans = dh;
  }
  cout << ans << endl;
}

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.at(i);
  sort(h.begin(), h.end());

  int ans = 0;
  for (int i = 0; i <= N - K; i++) {
    int dh = h.at(K - 1 + i) - h.at(i);
    if (i == 0)
      ans = dh;
    else if (ans > dh)
      ans = dh;
  }
  cout << ans << endl;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n; i++)
#define all(a) (a).begin(), (a).end()

int main() {
  int n, k;
  cin >> n >> k;
  vector<LL> h(n);
  rep(i, n) cin >> h[i];
  sort(all(h));
  LL ans = 101010101010;
  rep(i, n - k + 1) { ans = min(ans, h[n - k + i] - h[i]); }
  cout << ans << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using LL = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define rep1(i, n) for (int i = 1; i < n; i++)
#define all(a) (a).begin(), (a).end()

int main() {
  int n, k;
  cin >> n >> k;
  vector<LL> h(n);
  rep(i, n) cin >> h[i];
  sort(all(h));
  LL ans = 101010101010;
  rep(i, n - k + 1) { ans = min(ans, h[k - 1 + i] - h[i]); }
  cout << ans << endl;
}
Problem p03208 (Python)

Buggy submission (status: Time Limit Exceeded):
def solve():
    n, k = map(int, input().split())
    h = sorted(list(int(input()) for _ in range(n)))
    min_diff = 10**9
    for i in range(n - k + 1):
        part = h[i : i + k]
        diff = part[-1] - part[0]
        if diff < min_diff:
            min_diff = diff
    return min_diff


if __name__ == "__main__":
    print(solve())


Fixed submission:
def solve():
    n, k = map(int, input().split())
    h = sorted(list(int(input()) for _ in range(n)))
    return min(h[i + k - 1] - h[i] for i in range(n - k + 1))


if __name__ == "__main__":
    print(solve())
Problem p03208 (C++)

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

using namespace std;

int main() {
  int i, N, K, h[100000], dif;
  long long min = 1000000000;

  scanf("%d %d", &N, &K);
  for (i = 0; i < N; i++) {
    scanf("%d", &h[i]);
  }

  sort(h, h + N);

  for (i = 0; i < N; i++) {
    dif = h[i + K - 1] - h[i];
    if (dif <= min && dif >= 0) {
      min = dif;
    }
  }

  printf("%lld\n", min);

  return 0;
}

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

using namespace std;

int main() {
  int i, N, K, h[100000], dif;
  long long min = 1000000000;

  scanf("%d %d", &N, &K);
  for (i = 0; i < N; i++) {
    scanf("%d", &h[i]);
  }

  sort(h, h + N);

  for (i = 0; i < N - K + 1; i++) {
    dif = h[i + K - 1] - h[i];
    if (dif <= min && dif >= 0) {
      min = dif;
    }
  }

  printf("%lld\n", min);

  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void Main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> h(N);
  rep(i, N) { cin >> h.at(i); }
  sort(h.begin(), h.end());

  ll ans = INF;
  rep(i, N - K + 1) {
    auto start = h.begin() + i;
    auto end = h.begin() + i + K;
    ll tmp = *max_element(start, end) - *min_element(start, end);
    if (tmp < ans) {
      ans = tmp;
    }
  }
  cout << ans << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}


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

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

void Main() {
  ll N, K;
  cin >> N >> K;
  vector<ll> h(N);
  rep(i, N) { cin >> h.at(i); }
  sort(h.begin(), h.end());

  ll ans = INF;
  rep(i, N - K + 1) {
    auto start = h.begin() + i;
    auto end = h.begin() + i + K;
    ll tmp = h.at(i + K - 1) - h.at(i);
    if (tmp < ans) {
      ans = tmp;
    }
  }
  cout << ans << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using ll = long long;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define reps(i, x) for (ll i = 1; i <= (ll)(x); i++)
#define rrep(i, x) for (ll i = ((ll)(x)-1); i >= 0; i--)
#define rreps(i, x) for (ll i = (ll)(x); i > 0; i--)
#define all(x) (x).begin(), (x).end()
#define m0(x) memset(x, 0, sizeof(x))
#define vll vector<ll>
#define vi vector<int>
#define vpll vector<pair<ll, ll>>
#define vpi vector<pair<int, int>>
#define mod 1000000007
using namespace std;
ll h[10005];
ll dp[10005];
int main() {
  ll n, k;
  cin >> n >> k;
  reps(i, n) { cin >> h[i]; }
  sort(h, h + n + 1);

  ll mn = 1L << 60;
  reps(i, n - k + 1) { mn = min(mn, h[k - 1 + i] - h[i]); }
  cout << mn << endl;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <string.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using ll = long long;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define reps(i, x) for (ll i = 1; i <= (ll)(x); i++)
#define rrep(i, x) for (ll i = ((ll)(x)-1); i >= 0; i--)
#define rreps(i, x) for (ll i = (ll)(x); i > 0; i--)
#define all(x) (x).begin(), (x).end()
#define m0(x) memset(x, 0, sizeof(x))
#define vll vector<ll>
#define vi vector<int>
#define vpll vector<pair<ll, ll>>
#define vpi vector<pair<int, int>>
#define mod 1000000007
using namespace std;
ll h[100005];
ll dp[100005];
int main() {
  ll n, k;
  cin >> n >> k;
  reps(i, n) { cin >> h[i]; }
  sort(h, h + n + 1);

  ll mn = 1L << 60;
  reps(i, n - k + 1) { mn = min(mn, h[k - 1 + i] - h[i]); }
  cout << mn << endl;
}
Problem p03208 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int a[10001];
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  sort(a + 1, a + 1 + n);
  int sum = 0, s = 0;
  for (int i = 1; i <= k; i++)
    sum = a[k] - a[1];
  for (int i = 2; i <= n - k + 1; i++) {
    s = a[i + k - 1] - a[i];
    if (sum > s)
      sum = s;
  }
  cout << sum;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int a[100001];
int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  sort(a + 1, a + 1 + n);
  int sum = 0, s = 0;
  for (int i = 1; i <= k; i++)
    sum = a[k] - a[1];
  for (int i = 2; i <= n - k + 1; i++) {
    s = a[i + k - 1] - a[i];
    if (sum > s)
      sum = s;
  }
  cout << sum;
  return 0;
}
Problem p03208 (C++)

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

int a[100005];

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  int minn = a[n - 1] - a[0];
  for (int i = 0; i <= n - k; i++) {
    int f = i, b = n - k + i;
    minn = min(minn, a[b] - a[f]);
  }
  cout << minn;
}

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

int a[100005];

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  int minn = a[n - 1] - a[0];
  for (int i = 0; i <= n - k; i++) {
    int f = i, b = k + i - 1;
    minn = min(minn, a[b] - a[f]);
  }
  cout << minn;
}