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

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

int main() {
  /* Input */
  int X, N;
  cin >> X >> N;

  vector<int> ng(N);
  rep(i, N) { cin >> ng.at(i); }

  /* Main Logic */
  // Forbidden Listを除いた数列を作成
  vector<int> ok(100 - N + 2);
  int tmpIndex = 1;

  ok.at(0) = 0;
  rep(i, 100) {
    bool ngMatchFlag = false;
    /* NG Listに一致するものがあるかどうか */
    rep(j, N) {
      if (ng.at(j) == i + 1) {
        ngMatchFlag = true;
        break;
      }
    }
    if (!ngMatchFlag) {
      ok.at(tmpIndex) = i + 1;
      tmpIndex++;
    }
  }
  ok.at(tmpIndex) = 101;

  int preDiff = 100;
  int nowDiff = 100;
  int preI = 0;

  rep(nowI, 100 - N + 2) {
    nowDiff = abs(ok.at(nowI) - X);
    if ((nowDiff < preDiff) ||
        (nowDiff == preDiff && ok.at(nowI) < ok.at(preI))) {
      preDiff = nowDiff;
      preI = nowI;
    }
  }

  /* Output */
  cout << ok.at(preI) << endl;
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 96) >= this->size() (which is 96)


Fixed submission:

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

int main() {
  /* Input */
  int X, N;
  cin >> X >> N;

  vector<int> ng(N);
  rep(i, N) { cin >> ng.at(i); }

  /* Main Logic */
  // Forbidden Listを除いた数列を作成
  vector<int> ok(100 - N + 1);
  int tmpIndex = 1;

  ok.at(0) = 0;
  rep(i, 100) {
    bool ngMatchFlag = false;
    /* NG Listに一致するものがあるかどうか */
    rep(j, N) {
      if (ng.at(j) == i + 1) {
        ngMatchFlag = true;
        break;
      }
    }
    if (!ngMatchFlag) {
      ok.at(tmpIndex) = i + 1;
      tmpIndex++;
    }
  }
  ok.at(tmpIndex) = 101;

  // rep (i, 100-N+2) {
  //     cout << i << " " << ok.at(i) << endl;
  // }

  int preDiff = 100;
  int nowDiff = 100;
  int preI = 0;

  rep(nowI, 100 - N + 2) {
    nowDiff = abs(ok.at(nowI) - X);
    if ((nowDiff < preDiff) ||
        (nowDiff == preDiff && ok.at(nowI) < ok.at(preI))) {
      preDiff = nowDiff;
      preI = nowI;
    }
  }

  /* Output */
  cout << ok.at(preI) << endl;
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define pi pair<ll, ll>
#define pii pair<ll, pi>
#define pb push_back
#define mk make_pair
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int x, n;
  cin >> x >> n;
  bool vis[205];
  for (int i = 0; i < 204; i++)
    vis[i] = false;
  for (int i = 0; i < n; i++) {
    int y;
    cin >> y;
    vis[y] = true;
  }
  vector<pair<int, int>> v;
  for (int i = -200; i <= 200; i++) {
    if (!vis[i] || i <= 0)
      v.pb(mk(abs(x - i), i));
  }
  sort(v.begin(), v.end());
  int ans = v[0].fi;
  int res1 = 1e9;
  for (int i = 0; i < int(v.size()); i++) {
    if (v[i].fi == ans) {
      res1 = min(res1, v[i].se);
    }
  }
  cout << res1 << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define fi first
#define se second
#define pi pair<ll, ll>
#define pii pair<ll, pi>
#define pb push_back
#define mk make_pair
int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int x, n;
  cin >> x >> n;
  bool vis[107] = {false};
  for (int i = 0; i < n; i++) {
    int y;
    cin >> y;
    vis[y] = true;
  }
  vector<pair<int, int>> v;
  for (int i = 1; i <= 100; i++) {
    if (!vis[i])
      v.pb(mk(abs(x - i), i));
  }
  sort(v.begin(), v.end());
  int ans = v[0].fi;
  int res1 = 1e9;
  for (int i = 0; i < v.size(); i++) {
    if (v[i].fi == ans) {
      res1 = min(res1, v[i].se);
    }
  }
  cout << res1 << endl;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int x, n, p[110], d = 200, k;
  cin >> x >> n;
  for (int i = 0; i < n; ++i)
    cin >> p[i];
  vector<int> v(102);
  for (int i = 0; i < 102; i++) {
    v.at(i) = i;
  }
  for (int i = 0; i < n; i++) {
    int value_to_remove = p[i];
    v.erase(std::remove(v.begin(), v.end(), value_to_remove), v.end());
  }
  for (int i = 0; i < 102 - n; i++) {
    if (abs(x - v.at(i)) < d) {
      k = v.at(i);
      d = abs(x - v.at(i));
    }
  }
  cout << k << endl;
}


Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 96) >= this->size() (which is 96)


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int x, n, p[110], d = 200, k;
  cin >> x >> n;
  for (int i = 0; i < n; ++i)
    cin >> p[i];
  vector<int> v(101);
  for (int i = 0; i < 101; i++) {
    v.at(i) = i;
  }
  for (int i = 0; i < n; i++) {
    int value_to_remove = p[i];
    v.erase(std::remove(v.begin(), v.end(), value_to_remove), v.end());
  }
  for (int i = 0; i < 101; i++) {
    if (abs(x - v.at(i)) < d) {
      k = v.at(i);
      d = abs(x - v.at(i));
    }
  }
  cout << k << endl;
}
Problem p02641 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int main() {
  int X, N;
  cin >> X >> N;
  int p[N];
  for (int i = 0; i <= N - 1; i++) {
    cin >> p[i];
  }
  int z1[100] = {0};
  int z2[100] = {0};
  for (int i = 0; i <= N - 1; i++) {
    if (-1 < X - p[i])
      z1[X - p[i]] = 1;
    if (X - p[i] < 1)
      z2[-1 * (X - p[i])] = 1;
  }
  for (int i = 0; i <= 99; i++) {
    if (z1[i] == 0) {
      cout << X - i;
      break;
    } else if (z2[i] == 0) {
      cout << X + i;
      break;
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int X, N;
  int p[N];
  cin >> X >> N;
  for (int i = 0; 1; i == N - 1) {
    cin >> p[i];
  }
  int n = 0;
  while (100) {
    int f1 = 0;
    int f2 = 0;
    for (int i = 0; 1; N - 1) {
      if (p[i] == X - n)
        f1 = 1;
      else if (p[i] == X + n)
        f2 = 1;
    }
    if (f1 == 0) {
      cout << X - n;
      break;
    } else if (f2 == 0) {
      cout << X + n;
      break;
    } else {
      n++;
    }
  }

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

#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>

#define db double
#define vi vector<int>
#define pb push_back
#define fi first
#define se second
#define ll long long
#define ld long double
#define maxn 5005
#define mod 998244353
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vll vector<ll>
#define vpi vector<pii>
#define vpll vector<pll>
#define vv(z, a, b) vector<vector<int>> z(a, vector<int>(b))
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) begin(c), end(c)
using namespace std;

int main() {
  int x, n;
  cin >> x >> n;
  if (n == 0) {
    cout << x << endl;
    return 0;
  }
  vi a(n);
  rep(i, n) cin >> a.at(i);
  sort(all(a));
  int su = -1;
  rep(j, n) {
    if (a.at(j) == x) {
      su = j;
    }
  }
  if (su == -1) {
    cout << x << endl;
    return 0;
  }
  int w = 1;
  while (1) {
    if (su - w < 0) {
      cout << x - w << endl;
      return 0;
    }
    if (a.at(su - w) != x - w) {
      cout << x - w << endl;
      return 0;
    }
    if (su + w > n - 1) {
      cout << x + w << endl;
      return 0;
    }
    if (a.at(su + w) != x + w) {
      cout << x + w << endl;
      return 0;
    }
    w++;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <string>
#include <vector>

#define db double
#define vi vector<int>
#define pb push_back
#define fi first
#define se second
#define ll long long
#define ld long double
#define maxn 5005
#define mod 998244353
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vll vector<ll>
#define vpi vector<pii>
#define vpll vector<pll>
#define vv(z, a, b) vector<vector<int>> z(a, vector<int>(b))
#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(c) begin(c), end(c)
using namespace std;

int main() {
  int x, n;
  cin >> x >> n;
  if (n == 0) {
    cout << x << endl;
    return 0;
  }
  vi a(n);
  rep(i, n) cin >> a.at(i);
  sort(all(a));
  int su = -1;
  rep(j, n) {
    if (a.at(j) == x) {
      su = j;
    }
  }
  if (su == -1) {
    cout << x << endl;
    return 0;
  }
  int w = 1;
  while (1) {
    if (a.at(su - w) != x - w) {
      cout << x - w << endl;
      return 0;
    }
    if (a.at(su + w) != x + w) {
      cout << x + w << endl;
      return 0;
    }
    w++;
  }
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

int main(void) {
  int X, N;
  std::cin >> X >> N;

  std::vector<int> a(N);
  if (N == 0) {
    std::cout << X << std::endl;
    return 0;
  } else {
    for (int i = 0; i < N; i++) {
      std::cin >> a[i];
    }
  }

  if (std::find(a.begin(), a.end(), X) != a.end()) {

  } else {
    std::cout << X << std::endl;
    return 0;
  }

  int count = 0;
  while (1) {
    count++;
    int s = X - count;
    int t = X + count;
    if (std::find(a.begin(), a.end(), s) != a.end()) {

    } else {
      std::cout << s << std::endl;
      return 0;
    }

    if (std::find(a.begin(), a.end(), t) != a.end()) {

    } else {
      std::cout << t << std::endl;
      return 0;
    }
  }

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

int main(void) {
  int X, N;
  std::cin >> X >> N;

  std::vector<int> a(N);
  for (int i = 0; i < N; i++) {
    std::cin >> a[i];
  }

  std::sort(a.begin(), a.end());

  int l = a[0];
  int r = a[N - 1];
  int mid = (l + r) / 2;
  for (int i = 0; i < N; i++) {
    if (a[i] < X) {
      l = a[i];
    } else {
      break;
    }
  }

  std::cout << l << std::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;
  int s[100 + 2] = {0};
  int f;
  for (int i = 1; i <= n; i++) {
    cin >> f;
    s[f] = 1;
  }
  for (int i = 0;; i++) {
    if (s[x - i] == 0) {
      cout << x - i << endl;
      return 0;
    }
    if (s[x + i] == 0) {
      cout << x + i << endl;
      return 0;
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int x, n;
  cin >> x >> n;
  int s[100 + 1] = {0};
  int f;
  for (int i = 1; i <= n; i++) {
    cin >> f;
    s[f] = 1;
  }
  for (int i = 0;; i++) {
    if ((x - i) > 0 && s[x - i] == 0) {
      cout << x - i << endl;
      return 0;
    }
    if ((x + i) <= 100 && s[x + i] == 0) {
      cout << x + i << endl;
      return 0;
    }
  }
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>

using namespace std;

int main(void) {
  int x, n;
  cin >> x >> n;
  int p;
  bool pn[102];
  for (int i = 0; i < 102; i++) {
    pn[i] = true;
  }
  for (int i = 0; i < n; i++) {
    cin >> p;
    pn[p] = false;
  }
  if (n == 0) {
    cout << x << endl;
    return 0;
  }

  // 入力されたpiがpn[pi] = falseとなっている
  int ans = 0;
  int count = 0;
  while (ans == 0) {
    int shita = x - count;
    if (shita >= 0) {
      if (pn[shita] == true) {
        ans = shita;
        break;
      }
    }
    int ue = x + count;
    if (ue <= 101) {
      if (pn[ue] == true) {
        ans = ue;
        break;
      }
    }
    count++;
    if (count > 102) {
      break;
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main(void) {
  int x, n;
  cin >> x >> n;
  int p;
  bool pn[n];
  for (int i = 0; i < 101; i++) {
    pn[i] = true;
  }
  for (int i = 0; i < n; i++) {
    cin >> p;
    pn[p] = false;
  }
  if (n == 0) {
    cout << x << endl;
    return 0;
  }

  // 入力されたpiがpn[pi] = falseとなっている
  int ans = 0;
  int count = 0;
  while (ans == 0) {
    int shita = x - count;
    if (shita >= 1) {
      if (pn[shita] == true) {
        ans = shita;
        break;
      }
    }
    int ue = x + count;
    if (ue <= 100) {
      if (pn[ue] == true) {
        ans = ue;
        break;
      }
    }
    count++;
    if (count > 100) {
      break;
    }
  }
  cout << ans << endl;
}
Problem p02641 (C++) — buggy submission, verdict: Time Limit Exceeded

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

#define GO                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long int

const int maxr = 1e5 + 5;

int32_t main() {
  GO;
  int n, x;
  cin >> x >> n;
  set<int> s;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    s.insert(x);
  }
  int ans, diff = 1e17;
  for (int i = -200; i <= 200; i++) {
    if (s.find(i) == s.end() && diff > abs(x - i)) {
      ans = i;
      diff = abs(x - i);
    }
  }
  cout << ans << endl;
  return 0;
}

//!

Error: TLE

Fixed submission:

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

#define GO                                                                     \\
  ios::sync_with_stdio(0);                                                     \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define int long long int

const int maxr = 1e5 + 5;

int32_t main() {
  GO;
  int n, x;
  cin >> x >> n;
  set<int> s;
  for (int i = 0; i < n; i++) {
    int x;
    cin >> x;
    s.insert(x);
  }
  queue<int> q;
  q.push(x);
  while (!q.empty()) {
    int top = q.front();
    q.pop();
    if (s.find(top) != s.end()) {
      q.push(top - 1);
      q.push(top + 1);
    } else {
      cout << top << endl;
      return 0;
    }
  }
  return 0;
}

//!
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main(void) {
  // Your code here!
  int x, n;
  cin >> x >> n;
  map<int, int> p;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    p[a]++;
  }

  if (p[x] == 0) {
    cout << x << endl;
    return 0;
  }

  for (int i = 0; i < 102; i++) {
    if (x - i > -1) {
      if (p[x - i] == 0) {
        cout << x - i << endl;
        return 0;
      }
    }
    if (x + i < 103) {
      if (p[x + i] == 0) {
        cout << x + i << endl;
        return 0;
      }
    }
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, x) for (int i = 0; i < x; i++)
#define put(ans) cout << ans << endl;
#define reverse(s) reverse(s.begin(), s.end())

bool jugdg(int x[], int size, int t) {
  for (int i = 0; i < size; i++) {
    if (t == x[i])
      return false;
  }
  return true;
}

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

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

  int l[n];

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

  vector<pair<int, int>> ele;

  rep(i, 20) {
    if (jugdg(l, n, i + 1)) {
      ele.push_back(make_pair(abs(x - i - 1), i + 1));
    }
  }

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

  cout << ele[0].second << 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()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T> void chmin(T &a, const T &b) noexcept {
  if (b < a)
    a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
  if (a < b)
    a = b;
}
void debug_out() { cout << "\\n"; }
template <class T, class... Args>
void debug_out(const T &x, const Args &...args) {
  cout << x << " ";
  debug_out(args...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  int x, n;
  cin >> x >> n;
  vector<int> cnt(102, 0);
  rep(i, n) {
    int p;
    cin >> p;
    cnt[p]++;
  }
  if (cnt[x] == 0) {
    cout << x << endl;
    return 0;
  }
  int itr1 = x - 1, itr2 = x + 1;
  while (true) {
    if (itr1 >= 0 && cnt[itr1] == 0) {
      cout << itr1 << endl;
      return 0;
    }
    if (itr2 <= 101 && cnt[itr2] == 0) {
      cout << itr2 << endl;
      return 0;
    }
    itr1--;
    itr2++;
  }
  return 0;
}

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()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template <class T> void chmin(T &a, const T &b) noexcept {
  if (b < a)
    a = b;
}
template <class T> void chmax(T &a, const T &b) noexcept {
  if (a < b)
    a = b;
}
void debug_out() { cout << "\\n"; }
template <class T, class... Args>
void debug_out(const T &x, const Args &...args) {
  cout << x << " ";
  debug_out(args...);
}
#ifdef _DEBUG
#define debug(...) debug_out(__VA_ARGS__)
#else
#define debug(...)
#endif

int main() {
  std::cin.tie(nullptr);
  std::ios_base::sync_with_stdio(false);
  std::cout << std::fixed << std::setprecision(15);
  int x, n;
  cin >> x >> n;
  vector<int> cnt(101, 0);
  rep(i, n) {
    int p;
    cin >> p;
    cnt[p]++;
  }
  if (cnt[x] == 0) {
    cout << x << endl;
    return 0;
  }
  int itr1 = x - 1, itr2 = x + 1;
  while (true) {
    if (itr1 >= 1 && cnt[itr1] == 0) {
      cout << itr1 << endl;
      return 0;
    }
    if (itr2 <= 100 && cnt[itr2] == 0) {
      cout << itr2 << endl;
      return 0;
    }
    itr1--;
    itr2++;
  }
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int k, n, i;
  cin >> k >> n;
  int a[n];
  int b[100001] = {0};
  for (i = 0; i < n; i++) {
    cin >> a[i];
    b[a[i]]++;
  }
  for (i = 0; i < 100; i++) {
    if (b[k - i] == 0) {
      cout << k - i;
      break;
    }
    if (b[k + i] == 0) {
      cout << k + i;
      break;
    }
  }
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int k, n, i;
  cin >> k >> n;
  int a[n];
  int b[101] = {0};
  for (i = 0; i < n; i++) {
    cin >> a[i];
    b[a[i]]++;
  }
  for (i = 0; i < 100000; i++) {
    if (b[k - i] == 0 && k - i > 0 && k - i < 101) {
      cout << k - i;
      break;
    }
    if (b[k + i] == 0 && k + i > 0 && k + i < 101) {
      cout << k + i;
      break;
    }
  }
}
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;

int main() {
  int x, n;
  vector<int> p(100);
  cin >> x >> n;
  rep(i, n) { cin >> p.at(i); }

  int min = 1000;
  rep(i, 102) {
    bool exist = false;
    rep(j, n) {
      if (i == p.at(j))
        exist = true;
    }
    if (!exist && abs(x - i) < abs(x - min))
      min = i;
  }
  cout << min << endl;
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  int x, n;
  vector<int> p(100);
  cin >> x >> n;
  rep(i, n) { cin >> p.at(i); }
  int j = 0;
  while (n + j < 100) {
    p.at(n + j) = 102;
    j++;
  }

  sort(p.begin(), p.end());
  int l = 0;
  int min = 101;
  rep(i, 101) {
    if (i != p.at(l) && abs(x - min) > abs(x - i)) {
      min = i;
    } else
      l++;
  }
  cout << min << 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;
  cin >> N;
  vector<int> p(N);
  unordered_set<int> exist;

  if (N == 0) {
    cout << X << endl;
    return 0;
  }
  for (int i = 0; i < N; i++) {
    cin >> p[i];
    exist.insert(p[i]);
  }

  int mindiff = 1000;
  int minnum = 1000;
  sort(p.begin(), p.end());
  int j;
  for (j = -1; j <= max(p[p.size() - 1] + 1, X + 1); j++) {
    if (exist.count(j) == 0 and abs(X - j) < mindiff) {
      mindiff = abs(X - j);
      minnum = j;
    }
  }
  cout << minnum << endl;
}


Error: 0

Fixed submission:

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

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

  for (int i = 0; i < N; i++) {
    cin >> p[i];
    exist.insert(p[i]);
  }

  int mindiff = 1000;
  int minnum = 1000;
  sort(p.begin(), p.end());
  int j;
  for (j = 0; j <= max(p[p.size() - 1], X); j++) {
    if (exist.count(j) == 0 and abs(X - j) < mindiff) {
      mindiff = abs(X - j);
      minnum = j;
    }
  }
  cout << minnum << endl;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

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

#define ll long long
#define fo(i, a, n) for (ll i = a; i < n; i++)
#define rfo(i, n, a) for (ll i = n; i >= a; i--)
#define pb push_back
#define mp make_pair
#define F first
#define S second

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t, n, i, j, x;
  cin >> x >> n;
  vector<ll> a;
  // a.resize(n);
  fo(i, 0, n) {
    cin >> j;
    a.pb(j);
  }
  if (!n) {
    cout << x << endl;
    return 0;
  }
  sort(a.begin(), a.end());
  ll in = lower_bound(a.begin(), a.end(), x) - a.begin();
  if (a[in] != x) {
    cout << x << endl;
    return 0;
  }
  ll k = 1, n1 = 1e9, n2 = 1e9;
  rfo(i, in - 1, 0) {
    if (a[i] != x - k) {
      n1 = x - k;
      break;
      ;
    }
    k++;
  }
  k = 1;
  fo(i, in + 1, n) {
    if (a[i] != x + k) {
      n2 = x + k;
      break;
    }
    k++;
  }
  if (n1 == 1e9)
    n1 = a[0] - 1;
  if (n2 == 1e9)
    n2 = a[n - 1] + 1;
  ll mn1 = fabs(n1 - x), mn2 = fabs(n2 - x);
  if (mn1 == mn2) {
    cout << min(n1, n2) << endl;
    return 0;
  }
  if (mn1 < mn2)
    cout << n1 << endl;
  else
    cout << n2;
}


Error: WA

Fixed submission:

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

#define ll long long
#define fo(i, a, n) for (ll i = a; i < n; i++)
#define rfo(i, n, a) for (ll i = n; i >= a; i--)
#define pb push_back
#define mp make_pair
#define F first
#define S second

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ll t, n, i, j, x;
  cin >> x >> n;
  vector<ll> a;
  a.resize(n);
  fo(i, 0, n) cin >> a[i];
  if (!n) {
    cout << x << endl;
  }
  sort(a.begin(), a.end());
  ll in = lower_bound(a.begin(), a.end(), x) - a.begin();
  if (a[in] != x) {
    cout << x << endl;
    return 0;
  }
  ll k = 1, n1 = 1e9, n2 = 1e9;
  rfo(i, in - 1, 0) {
    if (a[i] != x - k) {
      n1 = x - k;
      break;
      ;
    }
    k++;
  }
  k = 1;
  fo(i, in + 1, n) {
    if (a[i] != x + k) {
      n2 = x + k;
      break;
    }
    k++;
  }
  if (n1 == 1e9)
    n1 = a[0] - 1;
  if (n2 == 1e9)
    n2 = a[n - 1] + 1;
  ll mn1 = fabs(n1 - x), mn2 = fabs(n2 - x);
  if (mn1 == mn2) {
    cout << min(n1, n2) << endl;
    return 0;
  }
  if (mn1 < mn2)
    cout << n1 << endl;
  else
    cout << n2;
}
Problem p02641 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main() {
  int X, N;
  int j = 0;
  int answer;
  int check = 0;
  cin >> X >> N;
  if (N == 0) {
    cout << X << endl;
  } else {
    vector<int> P(N);
    for (int i = 0; i < N; i++) {
      cin >> P.at(i);
    }
    while (check == 0) {
      for (int i = 0; i < N; i++) {
        if (X - j == P.at(i)) {
          break;
        } else if (i == N - 1) {
          check = 1;
          answer = X - j;
        }
      }
      for (int i = 0; i < N; i++) {
        if (X + j == P.at(i)) {
          break;
        } else if (i == N - 1 && check == 0) {
          check = 1;
          answer = X + j;
        }
      }
      j++;
    }
    cout << answer << endl;
  }
}

Error: TLE

Fixed submission:

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

int main() {
  int X, N;
  int j = 0;
  int answer;
  int check = 0;
  cin >> X >> N;
  vector<int> P(N);
  for (int i = 0; i < N; i++) {
    cin >> P.at(i);
  }
  while (check == 0) {
    for (int i = 0; i < N; i++) {
      if (X - j == P.at(i)) {
        break;
      } else if (i == N - 1) {
        check = 1;
        answer = X - j;
      }
    }
    for (int i = 0; i < N; i++) {
      if (X + j == P.at(i)) {
        break;
      } else if (i == N - 1 && check == 0) {
        check = 1;
        answer = X + j;
      }
    }
    j++;
  }
  cout << answer << endl;
}