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

#include <bits/stdc++.h>

using namespace std;
#define INF 1e18
typedef long long ll;
typedef pair<int, int> P;

int main() {
  int x, n;
  cin >> x >> n;
  if (n == 0) {
    cout << x << endl;
    return 0;
  }
  vector<int> p(n + 1);
  p.at(0) = -1e9;
  for (int i = 1; i <= n; i++) {
    cin >> p.at(i);
  }
  int count = 0;
  sort(p.begin(), p.end());
  for (int i = 0; i < n + 1; i++) {
    if (p.at(i) <= x) {
      count++;
    }
    if (p.at(i) > x) {
      continue;
    }
  }
  if (p.at(count - 1) != x) {
    cout << x << endl;
    return 0;
  }
  p.push_back(1e9);

  for (int i = 1; i < 200; i++) {
    if (x - i != p.at(count - 1 - i)) {
      cout << x - i << endl;
      return 0;
    }
    if (x + i != p.at(count - 1 + i)) {
      cout << x + i << endl;
      return 0;
    }
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define INF 1e18
typedef long long ll;
typedef pair<int, int> P;

int main() {
  int x, n;
  cin >> x >> n;
  if (n == 0) {
    cout << x << endl;
    return 0;
  }
  vector<int> p(n + 1);
  p.at(0) = 0;
  for (int i = 1; i <= n; i++) {
    cin >> p.at(i);
  }
  int count = 0;
  sort(p.begin(), p.end());
  for (int i = 0; i < n + 1; i++) {
    if (p.at(i) <= x) {
      count++;
    }
    if (p.at(i) > x) {
      continue;
    }
  }
  if (p.at(count - 1) != x) {
    cout << x << endl;
    return 0;
  }
  p.push_back(0);

  for (int i = 1; i < 200; i++) {
    if (x - i != p.at(count - 1 - i)) {
      cout << x - i << endl;
      return 0;
    }
    if (x + i != p.at(count - 1 + i)) {
      cout << x + i << endl;
      return 0;
    }
  }
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  int x, n, count, xx, ans;
  cin >> x >> n;
  if (n != 0) {
    vector<int> p(n), integer(102);
    for (int i = 0; i < n; i++) {
      cin >> p.at(i);
    }

    for (int i = 0; i < 102; i++) {
      integer.at(i) = i;
    }
    for (int i = 0; i < n; i++) {
      integer.at(p.at(i)) = -100;
    }

    count = 1;
    while (true) {
      xx = x;

      if (xx < 0 || 101 < xx) {
        ans = x;
        break;
      }
      if (integer.at(xx) != -100) {
        ans = xx;
        break;
      }

      xx -= count;

      if (xx < 0 || 101 < xx) {
        ans = x;
        break;
      }
      if (integer.at(xx) != -100) {
        ans = xx;
        break;
      }

      xx += 2 * count;
      if (xx < 0 || 101 < xx) {
        ans = x;
        break;
      }
      if (integer.at(xx) != -100) {
        ans = xx;
        break;
      }
      count++;
    }
  } else {
    ans = x;
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

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

int main() {
  int x, n, count, xx, ans;
  cin >> x >> n;
  if (n != 0) {

    vector<int> p(n), integer(101);
    for (int i = 0; i < n; i++) {
      cin >> p.at(i);
    }

    for (int i = 0; i < 101; i++) {
      integer.at(i) = i;
    }
    for (int i = 0; i < n; i++) {
      integer.at(p.at(i)) = -100;
    }

    count = 1;
    while (true) {
      xx = x;

      if (xx < 0 || 101 < xx) {
        ans = x;
        break;
      }
      if (integer.at(xx) != -100) {
        ans = xx;
        break;
      }

      xx -= count;

      if (xx < 0 || 101 < xx) {
        ans = x;
        break;
      }
      if (integer.at(xx) != -100) {
        ans = xx;
        break;
      }

      xx += 2 * count;
      if (xx < 0 || 101 < xx) {
        ans = x;
        break;
      }
      if (integer.at(xx) != -100) {
        ans = xx;
        break;
      }
      count++;
    }
  } else {
    ans = x;
  }

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

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int X, N;
  cin >> X >> N;
  if (N == 0) {
    cout << X << endl;
    return 0;
  }
  int tmp = 0;
  vector<int> P(N);
  for (int i = 0; i < N; i++) {
    cin >> tmp;
    P.at(i) = tmp;
  }
  sort(P.begin(), P.end());

  vector<int> P_non;
  bool flag = true;
  for (int i = 1; i <= 100; i++) {
    for (int j = 0; j < N; j++) {
      if (i == P.at(j))
        flag = false;
    }
    if (flag)
      P_non.push_back(i);
    flag = true;
  }
  P_non.push_back(0);
  P_non.push_back(101);
  sort(P_non.begin(), P_non.end());

  int diff = 1000;
  int num = 1000;
  for (unsigned int i = 0; i < P_non.size(); i++) {
    if (abs(P_non.at(i) - X) < diff) {
      diff = abs(P_non.at(i) - X);
      num = i;
    }
  }
  cout << P_non.at(num) << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int main() {
  int X, N;
  cin >> X >> N;
  if (N == 0) {
    cout << X << endl;
    return 0;
  }
  int tmp = 0;
  vector<int> P(N);
  for (int i = 0; i < N; i++) {
    cin >> tmp;
    P.at(i) = tmp;
  }
  sort(P.begin(), P.end());

  vector<int> P_non;
  bool flag = true;
  for (int i = 1; i <= 100; i++) {
    for (int j = 0; j < N; j++) {
      if (i == P.at(j))
        flag = false;
    }
    if (flag)
      P_non.push_back(i);
    flag = true;
  }

  int diff = 1000;
  int num = 1000;
  for (unsigned int i = 0; i < P_non.size(); i++) {
    if (abs(P_non.at(i) - X) < diff) {
      diff = abs(P_non.at(i) - X);
      num = i;
    }
  }
  cout << P_non.at(num) << endl;
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
#define int64 long long
void solve() { return; }
int main() {
  int x, n;
  cin >> x >> n;
  if (n == 0)
    cout << x << endl;
  else {
    int a[n];
    set<int> st;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      st.insert(a[i]);
    }
    for (int i = 0; i < x + 1; i++) {
      for (int j = -1; j < 2; j++) {
        int p = x + j * i;
        if (st.count(p) == 0) {
          cout << p << endl;
          return 0;
        }
      }
    }
  }
  return 0;
}


Error: 0

Fixed submission:

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

void solve() { return; }
int main() {
  int n, l;
  cin >> l >> n;
  if (n == 0) {
    cout << l << endl;
    return 1;
  }
  int a[n];

  set<int> st;
  // int max1=INT_MIN;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++)
    st.insert(a[i]);
  // cout<<max1;
  int res = INT_MAX, j;
  for (int i = 0; i <= 100; i++) {
    if (st.count(i))
      continue;
    // cout<<abs(l-i)<<"for "<<i<<endl;
    if (abs(l - i) < res)
      res = min(res, abs(l - i)), j = i;
  }
  cout << j << endl;

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

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

int main() {
  ll x, n;
  cin >> x >> n;
  ll q;
  vector<ll> p;
  ll y = 101;
  ll ans = 101;

  if (n == 0) {
    y = x;
  } else {
    for (ll i = 0; i < n; i++) {
      cin >> q;
      p.push_back(q);
    }
    for (ll i = -1; i < 102; i++) {
      auto itr = std::find(p.begin(), p.end(), i);
      if (itr == p.end()) {
        if (llabs(x - i) < ans) {
          ans = llabs(x - i);
          y = i;
        }
      }
    }
  }
  cout << y;

  return 0;
}


Error: -11

Fixed submission:

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

int main() {
  ll x, n;
  cin >> x >> n;
  vector<ll> p;
  ll y = 101;
  ll ans = 101;

  if (n == 0) {
    y = x;
  } else {
    for (ll i = 0; i < n; i++)
      cin >> p[i];
    for (ll i = -1; i < 102; i++) {
      auto itr = std::find(p.begin(), p.end(), i);
      if (itr != p.end()) {
        break;
      } else {
        if (llabs(x - i) < ans) {
          ans = llabs(x - i);
          y = i;
        }
      }
    }
  }
  cout << y;

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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
// #define f(i,a,n) for(ll i=a;i<n;i++)
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

int main() {
  fast;
  ll t = 1;
  // cin>>t;
  while (t--) {
    ll x, n, p, q, miin = 9999999999, k = 0;
    cin >> x >> n;
    ll A[n], B[1000] = {0}, ans = 99999999999;
    if (n == 0)
      cout << x;
    else {
      for (ll i = 0; i < n; i++) {
        cin >> A[i];
        B[A[i]]++;
      }
      for (ll i = 0; i <= 101; i++) {
        if (B[i] == 0) {

          // cout<<i<<" ";
          p = abs(x - i);
          // cout<<p<<" ";
          if (p <= miin) {
            if (p == miin) {
              miin = p;
              ans = min(i, ans);
              // cout<<ans<<" ";
            } else {
              miin = p;
              ans = i;
            }
          }
        }
      }
      cout << ans;
    }
  }
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
// #define f(i,a,n) for(ll i=a;i<n;i++)
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)

int main() {
  fast;
  ll t = 1;
  // cin>>t;
  while (t--) {
    ll x, n, p, q, min = 9999999999, k = 0;
    cin >> x >> n;
    ll A[n], B[2000] = {0}, C[1000] = {0}, ans = x;
    for (ll i = 0; i < n; i++) {
      cin >> A[i];
      B[A[i]]++;
    }
    sort(A, A + n);
    for (ll i = A[0]; i <= A[n - 1]; i++) {
      if (B[i] == 0) {
        C[k] = i;
        p = abs(x - i);
        if (p < min) {
          min = p;
          ans = i;
        }
      }
    }
    cout << ans;
  }
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define PER(i, a, b) for (ll i = (a); i >= (b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
#define ALL(a) (a).begin(), (a).end()
const ll INF = 1e18 + 18;
const ll MAX = 200000;
const ll MOD = 1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;
static const long double pi = 3.141592653589793;

int main() {
  ll X, N;
  cin >> X >> N;
  vector<ll> P(N);
  rep(i, N) { cin >> P[i]; }
  ll Min = INF;
  ll Ans = 0;
  for (ll i = -100; i <= 200; i++) {
    bool OK = true;
    rep(j, N) {
      if (i == P[j])
        OK = false;
    }
    if (!OK)
      continue;
    if (Min > abs(X - i)) {
      Min = abs(X - i);
      Ans = i;
    }
  }
  cout << Ans << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define PER(i, a, b) for (ll i = (a); i >= (b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
#define ALL(a) (a).begin(), (a).end()
const ll INF = 1e18 + 18;
const ll MAX = 200000;
const ll MOD = 1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;
static const long double pi = 3.141592653589793;

int main() {
  ll X, N;
  cin >> X >> N;
  if (N == 0) {
    cout << X << endl;
    return 0;
  }
  vector<ll> P(N);
  vector<bool> J(101, true);
  rep(i, N) {
    cin >> P[i];
    J[P[i]] = false;
  }
  ll Min = 101;
  for (ll i = 1; i <= 100; i++) {
    if (J[i]) {
      Min = min(Min, abs(i - X));
    }
  }
  if (J[X - Min]) {
    cout << X - Min << endl;
    return 0;
  }
  cout << X + Min << 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)
int main() {
  int x, n, m = 1000, record;
  cin >> x >> n;
  vector<int> vec(n);
  vector<int> v(1000);
  if (n == 0) {
    cout << x << endl;
    return 0;
  }
  rep(i, n) cin >> vec.at(i);
  rep(i, 102) {
    rep(j, n) {
      if (i == vec.at(j)) {
        v.at(i) = 1;
      }
    }
    if (v.at(i) == 1)
      continue;
    int a = abs(x - i);
    if (a < m) {
      record = i;
      m = min(m, a);
    }
  }
  cout << record << endl;
  // rep(i, 101-n) cout << S.at(i) << endl;
}


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


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
int main() {
  int x, n, m = 1000, record;
  cin >> x >> n;
  vector<int> vec;
  vector<int> vec2;
  if (n == 0) {
    cout << x << endl;
    return 0;
  }
  rep(i, n) cin >> vec.at(i);
  rep(i, 101) {
    rep(j, n) {
      if (i != vec.at(j)) {
        int a = abs(x - i);
        if (a < m) {
          record = i;
          m = min(m, a);
        }
      }
    }
  }
  cout << record << endl;
  // rep(i, 101-n) cout << S.at(i) << endl;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
#define ll long long int
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
#define ll long long int
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int mx6[] = {1, 0, -1, 1, -1, 0};
int my6[] = {1, 1, 1, 0, 0, -1};

int main() {
  int x, n;
  cin >> x >> n;
  vector<int> p(n);
  rep(i, n) cin >> p[i];
  sort(p.begin(), p.end());
  int X = x, ans1 = 100000, ans2 = 100000;

  bool ok;
  while (X > -200) {
    ok = false;
    for (int i = n - 1; i >= 0; i--) {
      if (p[i] == X) {
        ok = true;
        break;
      }
    }
    if (!ok) {
      ans1 = X;
      break;
    }
    X--;
  }
  X = x;
  while (X <= 200) {
    ok = false;
    for (int i = 0; i < n; i++) {
      if (p[i] == X) {
        ok = true;
        break;
      }
    }
    if (!ok) {
      ans2 = X;
      break;
    }
    X++;
  }
  if (abs(x - ans1) <= abs(x - ans2)) {
    cout << ans1 << endl;
  } else
    cout << ans2 << endl;
  // cout << ans2 << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
#define ll long long int
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <utility>
#include <vector>
#define ll long long int
#define pb push_back
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;

int mx6[] = {1, 0, -1, 1, -1, 0};
int my6[] = {1, 1, 1, 0, 0, -1};

int main() {
  int x, n;
  cin >> x >> n;
  vector<int> p(n);
  rep(i, n) cin >> p[i];
  sort(p.begin(), p.end());
  vector<int> a;
  for (int i = 1; i <= 100; i++) {
    if (!binary_search(p.begin(), p.end(), i)) {
      a.pb(i);
    }
  }

  int b = 1000000;
  rep(i, 100) b = min(b, abs(x - a[i]));

  if (!binary_search(p.begin(), p.end(), x - b))
    cout << x - b << endl;
  else
    cout << x + b << endl;
  // cout << x << endl;
  // cout << b << 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 ll = long long;
using P = pair<int, int>;

int main() {
  int x, n;

  cin >> x >> n;

  vector<int> vec(300);
  rep(i, n) {
    int a = 0;
    cin >> a;
    vec.at(a)++;
  }
  int ans = 200;

  for (int i = x; i >= 0; i--) {
    if (vec.at(i) == 0) {
      ans = i;
      break;
    }
  }
  for (int i = x; i < 210; i++) {
    if (vec.at(i) == 0 && ans == 200) {
      ans = i;
      break;
    } else if (vec.at(i) == 0 && ans != 200 && x - ans > i - x) {
      ans = i;
      break;
    }
  }

  cout << ans << endl;
}


Error: 0

Fixed submission:

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

int main() {
  int x, n;

  cin >> x >> n;

  vector<int> vec(102);
  rep(i, n) {
    int a = 0;
    cin >> a;
    vec.at(a)++;
  }
  int ans = 200;

  for (int i = x; i >= 0; i--) {
    if (vec.at(i) == 0) {
      ans = i;
      break;
    }
  }
  for (int i = x; i < 210; i++) {
    if (vec.at(i) == 0 && ans == 200) {
      ans = i;
      break;
    } else if (vec.at(i) == 0 && ans != 200 && x - ans > i - x) {
      ans = i;
      break;
    }
  }

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

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define LOL cout << '\\n';
#define MOD 1000000007
#define sz(x) ((int)x.size())
#define INF LONG_LONG_MAX
#define NINF LONG_LONG_MIN

const int N = 107;
int fr[N];

void solve() {
  int x, n;
  cin >> x >> n;
  int mi = 1000, ma = 0;
  for (int i = 0; i < N; i++)
    fr[i] = 0;
  for (int i = 0, a; i < n; i++)
    cin >> a, fr[a]++, mi = min(mi, a), ma = max(ma, a);
  if (fr[x] == 0) {
    cout << x;
    LOL return;
  }
  vector<int> v;
  for (int i = x - 1; i >= mi - 1; i--) {
    if (!fr[i]) {
      v.pb(i);
      break;
    }
  }
  for (int i = x + 1; i <= ma + 1; i++) {
    if (!fr[i]) {
      v.pb(i);
      break;
    }
  }
  if (sz(v) == 1) {
    cout << v[0];
    LOL return;
  }
  sort(all(v));
  if (abs(x - v[0]) <= abs(x - v[1])) {
    cout << v[0];
    LOL return;
  } else {
    cout << v[1];
    LOL
  }
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define all(x) x.begin(), x.end()
#define LOL cout << '\\n';
#define MOD 1000000007
#define sz(x) ((int)x.size())
#define INF LONG_LONG_MAX
#define NINF LONG_LONG_MIN

const int N = 107;
int fr[N];

void solve() {
  int x, n;
  cin >> x >> n;
  int mi = 1000, ma = 0;
  for (int i = 0; i < N; i++)
    fr[i] = 0;
  for (int i = 0, a; i < n; i++)
    cin >> a, fr[a]++, mi = min(mi, a), ma = max(ma, a);
  if (fr[x] == 0) {
    cout << x;
    LOL return;
  }
  vector<int> v;
  for (int i = x - 1; i >= mi; i--) {
    if (!fr[i]) {
      v.pb(i);
      break;
    }
  }
  for (int i = x + 1; i <= ma; i++) {
    if (!fr[i]) {
      v.pb(i);
      break;
    }
  }
  sort(all(v));
  if (abs(x - v[0]) <= abs(x - v[1])) {
    cout << v[0];
    LOL return;
  } else {
    cout << v[1];
    LOL
  }
}

signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  solve();
  return 0;
}
Problem p02641 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

int main() {
  int X, N, *p, dif = 1, num = -1;
  std::cin >> X >> N;
  p = new int[N + 2];
  for (int i = 0; i < N; i++) {
    std::cin >> p[i];
  }
  p[N] = -1000;
  p[N + 1] = 1000;
  std::sort(p, p + N + 2);
  for (int i = 1; i < N + 1; i++) {
    if (p[i] == X)
      num = i;
  }
  if (num == -1) {
    std::cout << X;
    exit(0);
  }
  while (true) {
    if (p[num - dif] != X - dif) {
      std::cout << X - dif;
      break;
    }
    if (p[num + dif] != X + dif) {
      std::cout << X + dif;
      break;
    }
    dif += 1;
  }
}

Error: WA

Fixed submission:

#include <algorithm>
#include <iostream>

int main() {
  int X, N, *p, dif = 1, num = -1;
  std::cin >> X >> N;
  p = new int[N + 2];
  for (int i = 0; i < N; i++) {
    std::cin >> p[i];
  }
  p[N + 1] = -1000;
  p[N + 2] = 1000;
  std::sort(p, p + N);
  for (int i = 0; i < N; i++) {
    if (p[i] == X)
      num = i;
  }
  if (num == -1) {
    std::cout << X;
    exit(0);
  }
  while (true) {
    if (p[num - dif] != X - dif) {
      std::cout << X - dif;
      break;
    }
    if (p[num + dif] != X + dif) {
      std::cout << X + dif;
      break;
    }
    dif += 1;
  }
}
Problem p02641 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define MOD 1000000007
#define mp make_pair
#define ll long long
#define pb push_back
#define faster                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define debug cout << "Debugging.." << endl
using namespace std;
int main() {
  faster;

  ll int num, n;
  cin >> num >> n;
  ll int x;
  set<int> v;
  for (ll int i = 0; i < n; i++) {
    cin >> x;
    v.insert(x);
  }
  int a, b;
  int s = 0;
  while (1) {
    a = num - s;
    b = num + s;
    if (v.find(a) == v.end()) {
      cout << a << endl;
      break;
    }

    if (v.find(b) == v.end()) {
      cout << b << endl;
      break;
    }
    s++;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define MOD 1000000007
#define mp make_pair
#define ll long long
#define pb push_back
#define faster                                                                 \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define debug cout << "Debugging.." << endl
using namespace std;
int main() {
  faster;

  ll int num, n;
  cin >> num >> n;
  ll int x;
  set<int> v;
  for (ll int i = 0; i < n; i++) {
    cin >> x;
    v.insert(x);
  }
  int a, b;
  queue<int> q;
  q.push(num);
  while (!q.empty()) {
    int f = q.front();
    q.pop();
    if (v.find(f) == v.end()) {
      cout << f << endl;
      break;
    }
    q.push(f - 1);
    q.push(f + 1);
  }
  return 0;
}