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

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  int x, n;
  int p[110], q[510], r[210];
  cin >> x >> n;
  int i, j, min = 10000000, ans;
  for (i = 1; i <= n; i++) {
    cin >> p[i];
  }
  for (i = 0; i <= 200; i++) {
    q[i] = i;
  }
  for (i = 201; i <= 400; i++) {
    q[i] = -(i - 200);
  }
  for (i = 1; i <= n; i++) {
    for (j = 0; j <= 400; j++) {
      if (p[i] == q[j]) {
        q[j] = 100000;
      }
    }
  }
  sort(q, q + 401);
  ans = x;
  for (j = 0; j <= 400; j++) {
    r[j] = abs(q[j] - x);
    if (min > r[j]) {
      min = r[j];
      ans = q[j];
    }
  }
  cout << ans << endl;
}


Error: -6
*** stack smashing detected ***: terminated


Fixed submission:

#include <algorithm>
#include <bits/stdc++.h>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>
using namespace std;
using ll = long long;

int main() {
  int x, n;
  int p[110], q[110], r[110];
  cin >> x >> n;
  int i, j, min = 10000000, ans;
  for (i = 1; i <= n; i++) {
    cin >> p[i];
  }
  for (i = 0; i <= 100; i++) {
    q[i] = i;
  }
  for (i = 101; i <= 200; i++) {
    q[i] = -i;
  }
  for (i = 1; i <= n; i++) {
    for (j = 0; j <= 200; j++) {
      if (p[i] == q[j]) {
        q[j] = 100000;
      }
    }
  }
  sort(q, q + 201);
  ans = x;
  for (j = 0; j <= 200; j++) {
    r[j] = abs(q[j] - x);
    if (min > r[j]) {
      min = r[j];
      ans = q[j];
    }
  }
  cout << ans << endl;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int x, n;
  cin >> x >> n;
  if (n == 0) {
    cout << x << endl;
    return 0;
  }
  set<int> a;
  rep(i, n) {
    int tmp;
    cin >> tmp;
    a.insert(tmp);
  }
  auto c = a.find(x);
  if (c == a.end()) {
    cout << x << endl;
    return 0;
  }
  int minus = 1;
  int plus = 1;
  while (true) {
    auto it = a.find(x - minus);
    auto ic = a.find(x + plus);
    if (it == a.end()) {
      cout << x - minus << endl;
      return 0;
    }
    if (ic == a.end()) {
      cout << x + plus << endl;
      return 0;
    }
    minus++;
    plus++;
  }
}

Error: 0

Fixed submission:

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

int main() {
  int x, n;
  cin >> x >> n;
  if (n == 0) {
    cout << x << endl;
    return 0;
  }
  vector<bool> ans(100, true);
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  rep(i, n) {
    a[i]--;
    ans[a[i]] = false;
  }
  if (ans[x - 1]) {
    cout << x << endl;
    return 0;
  }
  int minus = 1;
  int plus = 1;
  while (true) {
    if (x - 1 - minus >= -10) {
      if (ans[x - 1 - minus]) {
        cout << x - minus << endl;
        return 0;
      }
    }
    if (x - 1 + plus < 103) {
      if (ans[x - 1 + plus]) {
        cout << x + plus << endl;
        return 0;
      }
    }
    minus++;
    plus++;
  }
}
Problem p02641 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int x, n;
  cin >> x >> n;
  if (n == 0) {
    cout << x << endl;
  } else {
    int ans = 0;
    vector<int> p(n);
    for (int i = 0; i < n; i++) {
      cin >> p[i];
    }
    for (int i = 0; i < 102; i++) {
      int count = 0;
      for (int j = 0; j < n; j++) {
        if (i == p[j]) {
          count++;
        }
      }
      if (count == 0 && (i <= x || (x < i && x - ans > i - x))) {
        ans = i;
      }
    }
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

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

int main() {
  int x, n;
  cin >> x >> n;
  if (n == 0) {
    cout << x << endl;
  } else {
    int ans = 0;
    vector<int> p(n);
    for (int i = 0; i < n; i++) {
      cin >> p[i];
    }
    for (int i = 1; i < 101; i++) {
      int count = 0;
      for (int j = 0; j < n; j++) {
        if (i = p[j]) {
          count++;
        }
      }
      if (count == 0 && (i <= x || (x < i && x - ans > i - x))) {
        ans = i;
      }
    }
    cout << ans << endl;
  }
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

using namespace std;
const long long MOD = 1e18;

vector<pair<long long, long long>> factorize(long long n) {
  vector<pair<long long, long long>> a;

  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      int cnt = 0;
      while (n % i == 0) {
        n /= i;
        cnt++;
      }
      a.push_back(make_pair(i, cnt));
    }
  }
  if (n > 1)
    a.push_back(make_pair(n, 1));
  return a;
}

int main() {
  int x, n;
  cin >> x >> n;
  if (n == 0) {
    cout << x;
    return 0;
  }

  set<int> s;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    s.insert(x);
  }
  vector<int> missing;
  for (int i = 0; i <= 101; i++) {
    if (s.count(i))
      continue;
    missing.push_back(i);
  }

  int ans = missing[0];
  for (int i = 1; i < (int)missing.size(); i++) {
    if (abs(missing[i] - x) < abs(ans - x)) {
      ans = missing[i];
    }
  }
  cout << ans;

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
const long long MOD = 1e18;

vector<pair<long long, long long>> factorize(long long n) {
  vector<pair<long long, long long>> a;

  for (long long i = 2; i * i <= n; i++) {
    if (n % i == 0) {
      int cnt = 0;
      while (n % i == 0) {
        n /= i;
        cnt++;
      }
      a.push_back(make_pair(i, cnt));
    }
  }
  if (n > 1)
    a.push_back(make_pair(n, 1));
  return a;
}

int main() {
  int x, n;
  cin >> x >> n;
  if (n == 0) {
    cout << x;
    return 0;
  }

  set<int> s;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    s.insert(x);
  }
  vector<int> missing;
  for (int i = 1; i <= 100; i++) {
    if (s.count(i))
      continue;
    missing.push_back(i);
  }
  int ans = missing[0];
  for (int i = 1; i < (int)missing.size(); i++) {
    if (abs(missing[i] - x) < abs(ans - x)) {
      ans = missing[i];
    }
  }
  cout << ans;

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

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

int main() {
  int X, N, ans;
  cin >> X >> N;
  vector<bool> all(100, false);
  vector<int> p(N);

  for (int i = 0; i < N; i++) {
    cin >> p.at(i);
    all.at(p.at(i) - 1) = true;
  }
  // sort(p.begin(),p.end(),greater<int>());
  for (int i = 0; i < 101; i++) {
    if ((X - i - 1 >= 0) && (all.at(X - i - 1) == false)) {
      ans = X - i;
      break;
    } else if (X - i - 1 < 0) {
      ans = X - i;
      break;
    } else if ((X + i - 1 < 100) && (all.at(X + i - 1) == false)) {
      ans = X + i;
      break;
    } else if (X + i - 1 >= 100) {
      ans = X + i;
      break;
    }
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

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

int main() {
  int X, N, ans;
  cin >> X >> N;
  vector<bool> all(100, false);
  vector<int> p(N);

  for (int i = 0; i < N; i++) {
    cin >> p.at(i);
    all.at(p.at(i) - 1) = true;
  }
  // sort(p.begin(),p.end(),greater<int>());
  for (int i = 0; i < 101; i++) {
    if ((X - i - 1 >= 0) && (all.at(X - i - 1) == false)) {
      ans = X - i;
      break;
    } else if ((X + i - 1 < 101) && (all.at(X + i - 1) == false)) {
      ans = X + i;
      break;
    }
  }

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

#include <bits/stdc++.h>

using namespace std;

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

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

  if (n == 0) {
    cout << x << '\\n';
    return 0;
  }

  vector<int> p(n);
  for (int i = 0; i < n; ++i) {
    cin >> p[i];
  }
  sort(p.begin(), p.end());

  int cnt = 0;
  int dist = 999;
  int ans = -100;
  for (int i = -100; i <= 200; ++i) {
    if (i == p[cnt]) {
      ++cnt;
      continue;
    }
    if (dist > abs(x - i)) {
      dist = abs(x - i);
      ans = i;
    }
  }

  cout << ans << '\\n';

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int X, N;
  scanf("%d %d", &X, &N);
  vector<int> vec1(N);
  vector<int> vec2(100);

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

  for (int i = 0; i < N; i++) {
    vec2.at(i) = i + 1;
  }

  for (int i = 0; i < N; i++) {
    vec2.at(vec1.at(i)) = -1000;
  }

  if (N == 0) {
    cout << X;
    return 0;
  } else {
    int i = 0;
    int j = 0;
    while (X + i < 100) {
      if ((X + i) - vec2.at(X + i) == 0) {
        i++;
        break;
      }
    }
    while (X - j >= 0) {
      if ((X - j) - vec2.at(X - j) == 0) {
        j++;
        break;
      }
    }
    int A = X + i;
    int B = X - j;
    if (i < j) {
      cout << A;
    }
    if (i > j) {
      cout << B;
    }
    if (i == j) {
      cout << B;
    }
  }
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

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

#define REP(i, n) for (int i = 0; i < (n); i++)
#define REAP(i, a, n) for (int i = (a); i < (n); i++)
#define YES cout << "Yes" << endl
#define NO cout << "No" << endl
#define fr first
#define sc second
#define pb push_back
#define All(v) v.begin(), v.end()
typedef long long ll;

int main() {
  ios::sync_with_stdio(false);

  int x;
  int n;
  cin >> x >> n;
  vector<bool> flg(300, false);
  REP(i, n) {
    int p;
    cin >> p;
    flg[p + 100] = true;
  }

  int l, r;
  l = r = x + 100;

  while (true) {
    if (r >= 300 && l < 0)
      break;
    if (l >= 0 && flg[l] == false) {
      cout << l - 100 << endl;
      break;
    }
    if (r < 300 && flg[r] == false) {
      cout << r - 100 << endl;
      break;
    }
    l--;
    r++;
  }
}


Error: 0

Fixed submission:

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

#define REP(i, n) for (int i = 0; i < (n); i++)
#define REAP(i, a, n) for (int i = (a); i < (n); i++)
#define YES cout << "Yes" << endl
#define NO cout << "No" << endl
#define fr first
#define sc second
#define pb push_back
#define All(v) v.begin(), v.end()
typedef long long ll;

int main() {
  ios::sync_with_stdio(false);

  int x;
  int n;
  cin >> x >> n;
  vector<bool> flg(200, false);
  REP(i, n) {
    int p;
    cin >> p;
    flg[p - 1] = true;
  }

  int l, r;
  l = r = x - 1;
  while (true) {
    if (flg[l] == false) {
      cout << l + 1 << endl;
      break;
    }
    if (flg[r] == false) {
      cout << r + 1 << endl;
      break;
    }
    l--;
    r++;
    if (r >= n && l < 0)
      break;
  }
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

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

#define M 200005
#define MOD 1000000007
#define PI 3.14159265358979323846
#define ep emplace_back
#define mk make_pair
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()

typedef long long int lli;
typedef unsigned long long int ulli;
typedef pair<int, int> PII;
typedef pair<lli, lli> PLL;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

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

  map<int, int> mp;
  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    mp[temp]++;
  }

  vector<PII> res;
  for (int i = 0; i <= 1000; i++) {
    if (mp[i] == 0) {
      res.ep(mk(abs(i - x), i));
    }
  }

  sort(all(res));

  int ans = res[0].second;
  cout << ans << "\\n";
}

Error: 0

Fixed submission:

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

#define M 200005
#define MOD 1000000007
#define PI 3.14159265358979323846
#define ep emplace_back
#define mk make_pair
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()

typedef long long int lli;
typedef unsigned long long int ulli;
typedef pair<int, int> PII;
typedef pair<lli, lli> PLL;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);

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

  map<int, int> mp;
  for (int i = 0; i < n; i++) {
    int temp;
    cin >> temp;
    mp[temp]++;
  }

  vector<PII> res;
  for (int i = 1; i <= 100; i++) {
    if (mp[i] == 0) {
      res.ep(mk(abs(i - x), i));
    }
  }

  sort(all(res));

  int ans = res[0].second;
  cout << ans << "\\n";
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int x, n;
  cin >> x >> n;
  int h;
  map<long long, bool> ad;
  for (int i = 0; i < n; i++) {
    cin >> h;
    ad[h] = 1;
  }
  int l, u, ans = 0;
  l = u = x;
  while (1) {
    if (ad[l])
      l--;
    else {
      ans = l;
      break;
    };
    if (ad[u])
      u++;
    else {
      ans = u;
      break;
    };
  }
  cout << ans << endl;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int x, n;
  cin >> x >> n;
  vector<int> p(101);
  vector<int> ad(101);
  for (int i = 0; i < n; i++) {
    cin >> p.at(i);
    ad.at(i)++;
  }
  int l, u, a, ans = 0;
  l = u = x;
  while (1) {
    if (ad.at(l))
      l--;
    else {
      ans = l;
      break;
    };
    if (ad.at(u))
      u++;
    else {
      ans = u;
      break;
    };
  }
  cout << ans << endl;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int x, n, num, cnt = 0;
  vector<int> a;
  bool pos[110] = {0};
  cin >> x >> n;
  for (int i = 0; i < n; i++) {
    cin >> num;
    pos[num] = 1;
  }
  for (int i = 0; i <= 105; i++) {
    if (pos[i] == 0)
      a.push_back(i);
  }
  int ans = upper_bound(a.begin(), a.end(), x) - a.begin();
  if (a[ans] - x >= x - a[ans - 1]) {
    ans--;
  }
  printf("%d\\n", a[ans]);
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int x, n, num, cnt = 0;
  vector<int> a;
  bool pos[110] = {0};
  cin >> x >> n;
  for (int i = 0; i < n; i++) {
    cin >> num;
    pos[num] = 1;
  }
  for (int i = 1; i <= 100; i++) {
    if (pos[i] == 0)
      a.push_back(i);
  }
  int ans = lower_bound(a.begin(), a.end(), x) - a.begin();
  if (a.size() > 1) {
    if (a[ans] - x >= x - a[ans - 1]) {
      ans--;
    }
  }
  printf("%d\\n", a[ans]);
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>
// #include <omp.h>
#include <cstdio>
#include <memory>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long int ll;
const int MOD = 1000000007;
int main() {
  int X, N;
  int p;
  int n;
  int own = 0;
  int flagp = 0;
  int flagn = 0;
  int P[101];
  cin >> X >> N;
  rep(i, N) {
    cin >> P[i];
    if (X == P[i]) {
      own = 1;
    }
  }

  // X自身が解
  if (own == 0) {
    cout << X << endl;
    return 0;
  }
  p = X;
  n = X;
  rep(i, 101) {
    // cout << i << endl;
    p += 1;
    n -= 1;
    flagn = 0;
    flagp = 0;
    rep(j, N) {
      if (n == P[j]) {
        flagn = 1;
      } else if (p == P[j]) {
        flagp = 1;
      }
    }
    // cout << n << " " << p << endl;
    // cout << flagn << " " << flagp << " " << endl;
    if (flagn == 0) {
      // cout << "huga" << endl;
      cout << n << endl;
      break;
    } else if (flagp == 0) {
      // cout << "hoge" << endl;
      cout << p << endl;
      break;
    } else {
      // cerr << n << " " << flagn << ":" << p << " " << flagp << endl;
    }
  }
  return 0;
}

Error: 0
5 1:7 1


Fixed submission:

// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <fstream>
#include <iostream>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>
// #include <omp.h>
#include <cstdio>
#include <memory>

using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
typedef long long int ll;
const int MOD = 1000000007;
int main() {
  int X, N;
  int p = X;
  int n = X;
  int own = 0;
  int flagp = 0;
  int flagn = 0;
  int P[N + 1];
  cin >> X >> N;
  rep(i, N) {
    cin >> P[i];
    if (X == P[i]) {
      own = 1;
    }
  }

  // X自身が解
  if (own == 0) {
    cout << X << endl;
  }
  p = X;
  n = X;
  rep(i, 101) {
    // cout << i << endl;
    p += 1;
    n -= 1;
    flagn = 0;
    flagp = 0;
    rep(j, N) {
      if (n == P[j]) {
        flagn = 1;
      } else if (p == P[j]) {
        flagp = 1;
      }
    }
    // cout << n << " " << p << endl;
    // cout << flagn << " " << flagp << " " << endl;
    if (flagn == 0) {
      // cout << "huga" << endl;
      cout << n << endl;
      break;
    } else if (flagp == 0) {
      // cout << "hoge" << endl;
      cout << p << endl;
      break;
    } else {
      cerr << n << " " << flagn << ":" << p << " " << flagp << endl;
    }
  }
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define all(x) (x).begin(), (x).end()
#define dump(x) cout << #x << " = " << (x) << endl

using namespace std;
using ll = long long;

int main() {
  int x, n;
  cin >> x >> n;
  vector<int> p(n);
  rep(i, n) cin >> p[i];
  sort(all(p));

  vector<int> q;

  for (int i = 1; i <= 100; i++) {
    bool exist = false;
    rep(j, n) {
      if (p[j] == i) {
        exist = true;
        break;
      }
    }
    if (!exist)
      q.push_back(i);
  }

  q.push_back(0);
  q.push_back(101);

  for (int i = 0; i < q.size(); ++i)
    q[i] = abs(q[i] - x);

  sort(all(q));

  int ans1 = x - q[0];
  int ans2 = x + q[0];

  int ans = min(ans1, ans2);

  rep(i, n) {
    if (ans1 == p[i]) {
      ans = ans2;
      break;
    } else if (ans2 == p[i]) {
      ans = ans1;
      break;
    }
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); ++(i))
#define all(x) (x).begin(), (x).end()
#define dump(x) cout << #x << " = " << (x) << endl

using namespace std;
using ll = long long;

int main() {
  int x, n;
  cin >> x >> n;
  vector<int> p(n);
  rep(i, n) cin >> p[i];
  sort(all(p));

  vector<int> q;

  for (int i = 1; i <= 100; i++) {
    bool exist = false;
    rep(j, n) {
      if (p[j] == i) {
        exist = true;
        break;
      }
    }
    if (!exist)
      q.push_back(i);
  }

  for (int i = 0; i < q.size(); ++i) {
    q[i] = abs(q[i] - x);
  }
  sort(all(q));

  int ans1 = x - q[0];
  int ans2 = q[0] + x;

  int ans = -10000;

  rep(i, n) {
    if (ans1 == p[i]) {
      ans = ans2;
      break;
    } else if (ans2 == p[i]) {
      ans = ans1;
    }
  }

  if (ans == -10000)
    ans = min(ans1, ans2);

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

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

int main() {
  int x, n, z;
  cin >> x >> n;
  vector<int> d(102);
  for (int i = 0; i < n; i++) {
    int p;
    cin >> p;
    d[p] = 1;
  }
  pair<int, int> ans(99999, -1);
  for (int i = 0; i <= 101; ++i) {
    if (d[i] == 1)
      continue;
    int dif = abs(x - i);
    ans = min(ans, pair(dif, i));
  }
  cout << ans.second << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int x, n, z;
  cin >> x >> n;
  vector<int> a;
  for (int i = 0; i < n; i++) {
    cin >> z;
    a.push_back(z);
  }
  sort(a.begin(), a.end());
  int d = 999999, g = 0;
  for (int i = 0; i < n; i++) {
    if (d > abs(a[i] - x)) {
      g = i;
      d = abs(a[i] - x);
    }
  }
  int h = g;
  int l = g;
  int j = 0, f = 0;
  while (a[h] == x - j) {
    j++;
    h--;
  }
  while (a[l] == x + f) {
    f++;
    l++;
  }
  if (j < f)
    cout << x - j << endl;
  else
    cout << x + f << endl;

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

#include <bits/stdc++.h>
using namespace std;
int main() {
  int x, n, p, i, f, c, d, a = 0;
  map<int, int> mp;
  cin >> x >> n;
  for (i = 0; i < n; i++) {
    cin >> p;
    mp[p]++;
  }
  c = 1;
  d = 1;
  f = x;
  while (1) {
    if (!mp[f]) {
      cout << f;
      break;
    }
    a++;
    if (a % 2 != 0) {
      f = x - c;
      c++;
    } else {
      f = x + d;
      d++;
    }
  }
  return 0;
}

Error: WA

Fixed submission:

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

int main() {
  int x, n, a, s, p, i;
  vector<int> ar;
  cin >> x >> n;
  p = -1;
  for (i = 0; i < n; i++) {
    cin >> a;
    ar.push_back(a);
  }
  sort(ar.begin(), ar.end());
  for (i = 0; i < n; i++) {
    if (ar[i] == x)
      p = i;
  }
  if (p == -1)
    cout << x;
  if (p == 0)
    cout << x - 1;
  else {
    s = 1;
    while (1) {
      if (ar[p - s] != x - s) {
        cout << x - s;
        break;
      }

      if (ar[p + s] != x + s) {
        cout << x + s;
        break;
      }
      s++;
    }
  }
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <numeric>   // next_parmutation
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

int main() {
  int x, n;
  cin >> x >> n;
  vector<ll> p(n);
  rep(i, n) cin >> p[i];
  if (n == 0)
    cout << x << endl;
  else {
    ll mini = 100000000000;
    int ans;
    ll cal;

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

    rep(i, n) {
      cal = (x - p[i]);
      if (mini > abs(cal))
        ans = i;
      mini = min(mini, abs(cal));
    }

    int j = 1;

    while (1) {
      if (p[ans] - j != p[ans - j]) {
        cout << p[ans] - j << endl;
        break;
      }
      if (p[ans] + j != p[ans + j]) {
        cout << p[ans] + j << endl;
        break;
      }
      j++;
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <numeric>   // next_parmutation
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector
#define ll long long
#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

int main() {
  int x, n;
  cin >> x >> n;
  vector<ll> p(n);
  rep(i, n) cin >> p[i];

  ll mini = 100000000000;
  int ans;
  ll cal;

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

  rep(i, n) {
    cal = (x - p[i]);
    if (mini > abs(cal))
      ans = i;
    mini = min(mini, abs(cal));
  }

  int j = 1;

  while (1) {
    if (p[ans] - j != p[ans - j]) {
      cout << p[ans] - j << endl;
      break;
    }
    if (p[ans] + j != p[ans + j]) {
      cout << p[ans] + j << endl;
      break;
    }
    j++;
  }
}