alexjercan/bugnet huggingface dataset:
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int X, N;
  cin >> X >> N;

  if (N == 0) {
    cout << X;
    return 0;
  }

  vector<int> p(N);
  for (int i = 0; i < N; i++)
    cin >> p.at(i);

  sort(p.begin(), p.end());
  int x = 0;

  for (; x < N; x++) {
    if (p.at(x) == X)
      break;
  }

  if (x == N) {
    cout << X;
    return 0;
  }

  for (int i = 1; i <= N / 2; i++) {
    if (x - i < 0) {
      cout << X - i;
      return 0;
    }
    if (p.at(x - i) == X - i) {
      if (x + i >= N) {
        cout << X + i;
        return 0;
      }
      if (p.at(x + i) == X + i) {
        continue;
      } else {
        cout << X + i;
        return 0;
      }
    } else {
      cout << X - i;
      return 0;
    }
  }
  cout << p.at(0) - 1;
}

Error: WA

Fixed submission:

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

int main() {
  int X, N;
  cin >> X >> N;

  if (N == 0) {
    cout << X;
    return 0;
  }

  vector<int> p(N);
  for (int i = 0; i < N; i++)
    cin >> p.at(i);

  sort(p.begin(), p.end());
  int x = 0;
  for (; x < N; x++) {
    if (p.at(x) == X)
      break;
  }
  if (p.at(x) != X) {
    cout << X;
    return 0;
  }

  for (int i = 1; i <= N / 2; i++) {
    if (x - i < 0) {
      cout << X - i;
      return 0;
    }
    if (p.at(x - i) == X - i) {
      if (x + i >= N) {
        cout << X + i;
        return 0;
      }
      if (p.at(x + i) == X + i) {
        continue;
      } else {
        cout << X + i;
        return 0;
      }
    } else {
      cout << X - i;
      return 0;
    }
  }
}
Problem p02641 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int x, n, tmp, ans;
  vector<int> p;
  cin >> x >> n;
  if (n == 0) {
    cout << x << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    p.push_back(tmp);
  }
  sort(p.begin(), p.end());
  if (p[0] > x) {
    cout << x << endl;
    return 0;
  }
  if (p[0] == x) {
    cout << (x - 1) << endl;
    return 0;
  }
  for (int i = 1; i < n; i++) {
    if (p[i] > x) {
      cout << x << endl;
      return 0;
    } else if (p[i] == x) {
      int diff = 1;
      int fi = i + 1;
      int bi = i - 1;
      while (1) {
        if (bi >= 0) {
          if (p[bi] != (x - diff)) {
            cout << (x - diff) << endl;
            return 0;
          }
        } else {
          cout << (x - diff) << endl;
          return 0;
        }
        if (fi < n) {
          if (p[fi] != (x + diff)) {
            cout << (x + diff) << endl;
            return 0;
          }
        } else {
          cout << (x + diff) << endl;
          return 0;
        }
        diff++;
        fi++;
        bi--;
      }
    }
  }
  if (p[n - 1] == (x - 1))
    cout << (x + 1) << endl;
  else
    cout << (x - 1) << endl;
  return 0;
}


Error: TLE

Fixed submission:

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

int main() {
  int x, n, tmp, ans;
  vector<int> p;
  cin >> x >> n;
  if (n == 0) {
    cout << x << endl;
    return 0;
  }
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    p.push_back(tmp);
  }
  sort(p.begin(), p.end());
  if (p[0] > x) {
    cout << x << endl;
    return 0;
  }
  if (p[0] == x) {
    cout << (x - 1) << endl;
    return 0;
  }
  for (int i = 1; i < n; i++) {
    if (p[i] > x) {
      cout << x << endl;
      return 0;
    }
    if (p[i] == x) {
      int diff = 1;
      int fi = i + 1;
      int bi = i - 1;
      while (1) {
        if (bi >= 0) {
          if (p[bi] != (x - diff)) {
            cout << (x - diff) << endl;
            return 0;
          }
        }
        if (fi < n) {
          if (p[fi] != (x + diff)) {
            cout << (x + diff) << endl;
            return 0;
          }
        }
        diff++;
        fi++;
        bi--;
      }
    }
  }
  if (p[n - 1] == (x - 1))
    cout << (x + 1) << endl;
  else
    cout << (x - 1) << endl;
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const int MAX = 510000;
const double pi = acos(-1);
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
// int d[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

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

  int x, n;
  cin >> x >> n;

  int ans = 1e8;
  vector<int> p(n);
  set<int> st;
  rep(i, n) cin >> p[i];
  rep(i, n) st.insert(p[i]);
  for (int i = 101; i >= 0; i--) {
    if (st.count(i))
      continue;
    if (abs(ans - x) >= abs(i - x))
      ans = i;
  }
  cout << ans << endl;
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (long long i = 0; i < (long long)(n); i++)
#define all(a) a.begin(), a.end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

const ll MOD = 1e9 + 7;
const ll INF = 1e18;
const int MAX = 510000;
const double pi = acos(-1);
int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1};
int dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
// int d[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
vector<long long> calc_divisor(long long n) {
  vector<long long> res;
  for (long long i = 1LL; i * i <= n; ++i) {
    if (n % i == 0) {
      res.push_back(i);
      long long j = n / i;
      if (j != i)
        res.push_back(j);
    }
  }
  sort(res.begin(), res.end());
  return res;
}

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

  ll n;
  cin >> n;
  vector<ll> vec(n);
  rep(i, n) cin >> vec[i];
  sort(vec.begin(), vec.end());
  map<ll, ll> mp;
  rep(i, n) {
    bool ok = true;
    vector<ll> div = calc_divisor(vec[i]);
    if (div.size() >= mp.size()) {
      for (auto p : mp) {
        if (vec[i] % p.first == 0) {
          if (p.first == vec[i])
            continue;
          ok = false;
          break;
        }
      }
    } else {
      for (auto d : div) {
        if (mp[d] > 0) {
          if (d == vec[i])
            continue;
          ok = false;
          break;
        }
      }
    }
    if (ok)
      mp[vec[i]]++;
  }
  ll ans = 0;
  for (auto p : mp) {
    if (p.second == 1)
      ans++;
  }
  cout << ans << endl;
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int x, n;
  cin >> x >> n;
  vector<int> a(200, 0);
  if (n == 0) {
    cout << x << endl;
    return 0;
  }
  for (int i = 1; i <= n; ++i) {
    int p;
    cin >> p;
    a[p + 100] = 1;
  }

  if (a[x + 100] != 1) {
    cout << x << endl;
    return 0;
  }

  for (int i = 1; i <= 100; ++i) {
    int l = x + 100 - i;
    int r = x + 100 + i;
    if (a[l] != 1) {
      cout << l - 100 << endl;
      return 0;
    }
    if (a[r] != 1) {
      cout << r - 100 << endl;
      return 0;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>

using namespace std;

int main() {
  int x, N, index = 0, a = 0;
  int p[110];
  pair<int, int> min;
  cin >> x >> N;
  if (N == 0) {
    a = x;
  }
  for (int i = 1; i <= N; i++) {
    cin >> p[i];
  }
  sort(p, p + N);
  for (int i = 1; i <= N; i++) {
    if (p[i] == x)
      index = i;
  }
  for (int i = index; i = 1; i--) {
    for (int j = 1; j < index; j++) {
      min.first = x + j;
      min.second = x - j;
      if (binary_search(p, p + N, min.first)) {
        a = min.first;
      } else if (binary_search(p, p + N, min.second)) {
        a = min.second;
      } else if (binary_search(p, p + N, min.first) &&
                 binary_search(p, p + N, min.second)) {
        a = min.second;
      }
    }
  }
  cout << a << endl;
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int n, x;
  cin >> x >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  sort(v.begin(), v.end());
  int ans = -1, diff = 1001;
  if (n > 0) {
    for (int i = 0; i <= v[n - 1] + 1; i++) {
      if (binary_search(v.begin(), v.end(), i))
        continue;
      if (abs(x - i) < diff) {
        diff = abs(x - i);
        ans = i;
      }
    }
  }
  if (ans == -1)
    cout << x;
  else
    cout << ans;
  return 0;
}


Error: WA

Fixed submission:

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

int main() {
  int n, x;
  cin >> x >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++)
    cin >> v[i];
  sort(v.begin(), v.end());
  int ans = -1, diff = 1001;
  for (int i = 1; i <= v[n - 1] + 1; i++) {
    if (binary_search(v.begin(), v.end(), i))
      continue;
    if (abs(x - i) < diff) {
      diff = abs(x - i);
      ans = i;
    }
  }
  if (ans == -1)
    cout << x;
  else
    cout << ans;
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long int
#define loop(i, a, b) for (i = a; i < b; i++)
#define INF 10000000000
#define pb push_back
#define lim 100001

using namespace std;
int main() {
  ll x, n, i, j, ans = 0, diff = INF;

  cin >> x >> n;
  ll a[n];
  loop(i, 0, n) cin >> a[i];

  loop(i, 0, 102) {
    loop(j, 0, n) if (a[j] == i) break;
    if (j == n && diff > abs(x - i)) {
      ans = i;
      diff = abs(x - i);
    }
  }

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


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define loop(i, a, b) for (i = a; i < b; i++)
#define INF 10000000000
#define pb push_back
#define lim 100001

using namespace std;
int main() {
  ll x, n, i, j, ans = 0, diff = INF;
  ll a[n];
  cin >> x >> n;

  loop(i, 0, n) cin >> a[i];

  loop(i, 0, 102) {
    loop(j, 0, n) if (a[j] == i) break;
    if (j == n && diff > abs(x - i)) {
      ans = i;
      diff = abs(x - i);
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FORBIDDEN 300

int main(void) {
  int x, n, tmp, ans, j, k, f = 0;
  cin >> x >> n;
  if (n == 0) {
    ans = x;
  } else {
    int num = 103;
    int p[num];
    rep(i, num) { p[i] = i - 1; }
    rep(i, n) {
      cin >> tmp;
      p[tmp + 1] = FORBIDDEN;
    }
    for (j = x, k = x;; j--, k++) {
      if (p[j + 1] != FORBIDDEN) {
        ans = j;
        break;
      } else if (p[k + 1] != FORBIDDEN) {
        ans = k;
        break;
      }
    }
  }

  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

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

int main(void) {
  int x, n, tmp;
  vector<int> q;
  rep(i, 100) { q.push_back(i + 1); }
  cin >> x >> n;
  if (n > 0) {
    rep(i, n) {
      cin >> tmp;
      q.at(tmp - 1) = 0;
    }
  }
  int j, k;
  for (j = x, k = x; j >= 0; j--, k++) {
    if (q.at(j - 1) != 0) {
      cout << q.at(j - 1) << endl;
      break;
    } else if (q.at(k - 1) != 0) {
      cout << q.at(k - 1) << endl;
      break;
    }
  }

  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(ri, n) for (int ri = (int)(n - 1); ri >= 0; ri--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

int main() {
  vector<bool> f(104, false);
  int x, n;
  cin >> x >> n;
  rep(i, n) {
    int p;
    cin >> p;
    f.at(p) = true;
  }
  vector<int> a;
  rep(i, f.size()) {
    if (f.at(i) == false)
      a.push_back(i);
  }
  auto itr = lower_bound(a.begin(), a.end(), x);
  if (itr != a.begin()) {
    auto itm = itr;
    itm--;
    if (itr == a.end() || abs(*itm - x) <= abs(*itr - x))
      itr = itm;
  }
  cout << *itr << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rrep(ri, n) for (int ri = (int)(n - 1); ri >= 0; ri--)
#define rep2(i, x, n) for (int i = (int)(x); i < (int)(n); i++)
#define repit(itr, x) for (auto itr = x.begin(); itr != x.end(); itr++)
#define rrepit(ritr, x) for (auto ritr = x.rbegin(); ritr != x.rend(); ritr++)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using namespace std;

int main() {
  vector<bool> f(101, false);
  int x, n;
  cin >> x >> n;
  rep(i, n) {
    int p;
    cin >> p;
    f.at(p) = true;
  }
  vector<int> a;
  rep2(i, 1, 101) {
    if (f.at(i) == false)
      a.push_back(i);
  }
  auto itr = lower_bound(a.begin(), a.end(), x);
  if (itr != a.begin()) {
    auto itm = itr;
    itm--;
    if (abs(*itm - x) <= abs(*itr - x))
      itr = itm;
  }
  cout << *itr << endl;
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout.precision(30);
#define ll long long
#define pi 3.14159265359
using namespace std;

int main() {
  IOS;
  int x, n;
  cin >> x >> n;
  if (n == 0)
    return cout << x, 0;
  int p[n];
  int t = 0, ta, tb;
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    if (p[i] == x)
      t = 1;
  }
  if (t == 0)
    return cout << x, 0;
  int a = x, b = x;
  while (true) {
    a--;
    b++;
    ta = 0;
    tb = 0;
    for (int i = 0; i < n; i++) {
      if (p[i] == a)
        ta = 1;
      if (p[i] == b)
        tb = 1;
    }
    if (!ta)
      return cout << a, 0;
    if (!tb)
      return cout << b, 0;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define IOS                                                                    \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);                                                                 \\
  cout.precision(30);
#define ll long long
#define pi 3.14159265359
using namespace std;

int main() {
  IOS;
  int x, n;
  cin >> x >> n;
  if (n == 0)
    return cout << x, 0;
  int p[n];
  int t = 0, ta = 0, tb = 0;
  for (int i = 0; i < n; i++) {
    cin >> p[i];
    if (p[i] == x)
      t = 1;
  }
  if (t == 0)
    return cout << x, 0;
  int a = x, b = x;
  while (1) {
    a--;
    b++;
    for (int i = 0; i < n; i++) {
      if (p[i] == a)
        ta = 1;
      if (p[i] == b)
        tb = 1;
    }
    if (!ta)
      return cout << a, 0;
    if (!tb)
      return cout << b, 0;
  }
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define ll long long

using namespace std;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int x, n, idx = 0;
  cin >> x >> n;
  if (n == 0) {
    cout << x;
    return 0;
  }
  vector<int> banned(n);
  for (int i = 0; i < n; i++) {
    cin >> banned[i];
  }
  sort(banned.begin(), banned.end());
  while (idx < n && banned[idx] != x) {
    idx++;
  }
  if (banned[idx] != x) {
    cout << x;
    return 0;
  } else {
    // Find smallest lim
    int k = 0;
    while (idx - k >= 0 && banned[idx - k] == x - k) {
      k++;
    }
    int small_no = x - k;
    // Find biggest lim
    k = 0;
    while (idx + k < n && banned[idx + k] == x + k) {
      k++;
    }
    int big_no = x + k;
    if (big_no - x < x - small_no) {
      cout << big_no;
    } else {
      cout << small_no;
    }
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define ll long long

using namespace std;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int x, n, idx = 0, x_idx = -1;
  cin >> x >> n;
  vector<int> banned(n);
  for (int i = 0; i < n; i++) {
    cin >> banned[i];
  }
  sort(banned.begin(), banned.end());
  while (banned[idx] != x && idx < n) {
    idx++;
  }
  if (banned[idx] != x) {
    cout << x;
    return 0;
  } else {
    // Find smallest lim
    int k = 0;
    while (banned[idx - k] == x - k) {
      k++;
    }
    int small_no = x - k;
    // Find biggest lim
    k = 0;
    while (banned[idx + k] == x + k) {
      k++;
    }
    int big_no = x + k;
    if (big_no - x < x - small_no) {
      cout << big_no;
    } else {
      cout << small_no;
    }
  }
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;

void swap(int *xp, int *yp) {
  int temp = *xp;
  *xp = *yp;
  *yp = temp;
}

// A function to implement bubble sort
void bubbleSort(int arr[], int n) {
  int i, j;
  for (i = 0; i < n - 1; i++) {
    // Last i elements are already in place
    for (j = 0; j < n - i - 1; j++)
      if (arr[j] > arr[j + 1])
        swap(&arr[j], &arr[j + 1]);
  }
}

int search(int arr[], int size, int x) {
  for (int i = 0; i < size; i++) {
    if (arr[i] == x) {
      return 1;
    }
  }
  return -1;
}

int main() {
  int x, n;
  cin >> x;
  cin >> n;
  int *nums = (int *)malloc(n * sizeof(int));
  for (int i = 0; i < n; i++) {
    cin >> nums[i];
  }

  bubbleSort(nums, n);

  if (search(nums, n, x) == -1) {
    cout << x;
    return 0;
  }
  bool stop = 0;
  int i = 1;
  while (!stop) {
    int tempLeft = search(nums, n, x - i);
    int tempRight = search(nums, n, x + i);
    if (tempLeft == -1 && tempRight == -1) {
      cout << min(x + i, x - i);
      i++;
      return 0;
    } else if (tempLeft != -1 && tempRight == -1) {
      cout << x + i;
      i++;
      return 0;
    } else if (tempLeft == -1 && tempRight != -1) {
      cout << x - i;
      i++;
      return 0;
    } else {
      i++;
      continue;
    }
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

void swap(int *xp, int *yp) {
  int temp = *xp;
  *xp = *yp;
  *yp = temp;
}

// A function to implement bubble sort
void bubbleSort(int arr[], int n) {
  int i, j;
  for (i = 0; i < n - 1; i++) {
    // Last i elements are already in place
    for (j = 0; j < n - i - 1; j++)
      if (arr[j] > arr[j + 1])
        swap(&arr[j], &arr[j + 1]);
  }
}

int search(int arr[], int size, int x) {
  for (int i = 0; i < size; i++) {
    if (arr[i] == x) {
      return 1;
    }
  }
  return -1;
}

int main() {
  int x, n;
  int *nums;
  cin >> x;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> nums[i];
  }

  bubbleSort(nums, n);

  if (search(nums, n, x) == -1) {
    cout << x;
    return 0;
  }
  bool stop = 0;
  int i = 1;
  while (!stop) {
    int tempLeft = search(nums, n, x - i);
    int tempRight = search(nums, n, x + i);
    if (tempLeft == -1 && tempRight == -1) {
      cout << min(x + i, x - i);
      i++;
      return 0;
    } else if (tempLeft != -1 && tempRight == -1) {
      cout << x + i;
      i++;
      return 0;
    } else if (tempLeft == -1 && tempRight != -1) {
      cout << x - i;
      i++;
      return 0;
    } else {
      i++;
      continue;
    }
  }
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;

int main(void) {
  int x, n, y;
  cin >> x >> n;

  set<int> p;
  for (int i = 0; i < n; ++i) {
    cin >> y;
    p.insert(y);
  }

  for (int i = 0; i < x + 1; ++i) {
    for (auto dir : {-1, +1}) {
      y = x + dir * i;
      if (p.find(y) == p.end()) {
        cout << y << endl;
        return 0;
      }
    }
  }

  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  int x, n;
  cin >> x >> n;
  vector<int> p(n), d(n);
  int dmin = 101;
  int a = x;

  for (int i = 1; i <= n; i++) {
    cin >> p[i];

    if (p[i] >= x) {
      d[i] = p[i] - x;
    } else {
      d[i] = x - p[i];
    }

    if (dmin > d[i]) {
      dmin = d[i];
      a = p[i];
    } else if (dmin == d[i] && p[i] < x) {
      a = p[i];
    }
  }
  cout << a << endl;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

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

  if (n == 0) {
    cout << x << endl;
  } else {
    vector<int> p(n);
    for (int i = 0; i < n; i++) {
      cin >> p[i];
    }

    int closest, minAbs;
    minAbs = 1000;
    for (int i = 0; i <= 101; i++) {
      if (find(p.begin(), p.end(), i) == p.end()) {
        if (abs(x - i) < minAbs) {
          closest = i;
          minAbs = abs(x - i);
        }
      }
    }

    cout << closest << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;

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

  vector<int> all(100);
  vector<int> p(n);
  vector<int> np(100 - n);

  for (int i = 0; i < 100; i++) {
    all[i] = i + 1;
  }
  for (int i = 0; i < n; i++) {
    cin >> p[i];
  }

  sort(p.begin(), p.end());
  set_difference(all.begin(), all.end(), p.begin(), p.end(), np.begin());

  if (n == 0) {
    cout << x << endl;
  } else {
    int closestInt = np[0];
    int minAbs = abs(x - np[0]);

    for (int i = 0; i < (int)np.size(); i++) {
      if (abs(x - np[i]) < minAbs) {
        closestInt = np[i];
        minAbs = abs(x - np[i]);
      }
    }
    cout << closestInt << endl;
  }

  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int X, N;
  cin >> X >> N;
  vector<int> p(N);

  for (int i = 0; i < N; i++) {
    cin >> p.at(i);
  }

  if (N == 0) {
    cout << X << endl;
  }

  for (int i = 1; i <= N; i++) {

    int T1 = X - i;
    int T2 = X + i;
    int cnt1 = 0;
    int cnt2 = 0;
    int cnt = 0;

    for (int j = 0; j < N; j++) {
      if (T1 == p.at(j)) {
        cnt1++;
      } else if (T2 == p.at(j)) {
        cnt2++;
      } else if (X == p.at(j)) {
        cnt++;
      }
    }

    if (cnt == 0) {
      cout << X << endl;
      return 0;
    }

    else if (cnt1 == 0) {
      cout << T1 << endl;
      return 0;
    } else if (cnt2 == 0) {
      cout << T2 << endl;
      return 0;
    }
  }
}

Error: 0

Fixed submission:

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

int main() {
  int X, N;
  cin >> X >> N;
  vector<int> p(N);

  if (N == 0) {
    cout << X << endl;
  }

  for (int i = 0; i < N; i++) {
    cin >> p.at(i);
  }

  for (int i = 1; i <= N; i++) {

    int T1 = X - i;
    int T2 = X + i;
    int cnt1 = 0;
    int cnt2 = 0;
    for (int j = 0; j < N; j++) {
      if (T1 == p.at(j)) {
        cnt1++;
      } else if (T2 == p.at(i)) {
        cnt2++;
      }
    }

    if (cnt1 == 0) {
      cout << T1 << endl;
      return 0;
    } else if (cnt2 == 0) {
      cout << T2 << endl;
      return 0;
    }
  }
}