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


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

#define ff first
#define ss second
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<ll, ll>
#define vi vector<ll>
#define mii map<ll, ll>
#define pqb priority_queue<ll>
#define pqs priority_queue<ll, vi, greater<ll>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \\
  ll x;                                                                        \\
  cin >> x;                                                                    \\
  while (x--)
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define mx 3000
ll n, m;

int main() {

  FIO;
  ll a, c, d, e, f = 0, sum = 0, ck, k, p, q, r, t, add = 0, w, x, y, z;
  cin >> n >> x;
  unordered_set<int> s;
  for (int i = 0; i < x; i++) {
    cin >> a;
    s.insert(a);
  }
  if (s.find(n) == s.end()) {
    cout << n;
    return 0;
  }
  int i = 1;
  while (1) {
    if (s.find(n - i) == s.end()) {
      cout << n - i;
      return 0;
    }
    if (s.find(n + i) == s.end()) {
      cout << n + i;
      return 0;
    }
    i++;
  }
}


Error: 0

Fixed submission:


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

#define ff first
#define ss second
#define ll long long
#define pb push_back
#define mp make_pair
#define pii pair<ll, ll>
#define vi vector<ll>
#define mii map<ll, ll>
#define pqb priority_queue<ll>
#define pqs priority_queue<ll, vi, greater<ll>>
#define setbits(x) __builtin_popcountll(x)
#define zrobits(x) __builtin_ctzll(x)
#define mod 1000000007
#define inf 1e18
#define ps(x, y) fixed << setprecision(y) << x
#define mk(arr, n, type) type *arr = new type[n];
#define w(x)                                                                   \\
  ll x;                                                                        \\
  cin >> x;                                                                    \\
  while (x--)
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define mx 3000
ll n, m;
vector<ll> p;
void printUncommon(ll v[], ll v1[], ll n1, ll n2) {

  int i = 0, j = 0, k = 0;
  while (i < n1 && j < n2) {

    // If not common, print smaller
    if (v[i] < v1[j]) {
      p.pb(v[i]);
      i++;
      k++;
    } else if (v1[j] < v[i]) {
      p.pb(v1[j]);
      k++;
      j++;
    }

    // Skip common element
    else {
      i++;
      j++;
    }
  }

  // printing remaining elements
  while (i < n1) {
    p.pb(v[i]);
    i++;
    k++;
  }
  while (j < n2) {
    p.pb(v1[j]);
    j++;
    k++;
  }
}

int main() {
  FIO;
  ll c, d, e, f = 0, sum = 0, ck, k, q, r, t, add = 0, w, x, y, z;

  cin >> x >> m;
  ll v[m];
  for (int i = 0; i < m; i++) {
    cin >> v[i];
  }
  sort(v, v + m);
  w = v[m - 1] - v[0] + 1;
  ll v1[w];
  ll jj = v[0];
  for (int j = 0; j < w; j++) {
    v1[j] = jj;
    jj++;
  }

  printUncommon(v, v1, m, w);
  y = INT_MAX;
  for (int ii = 0; ii < p.size(); ii++) {
    //   cout<<p[ii]<<" ";
    ll y1 = abs(x - p[ii]);
    if (y1 < y) {
      y = y1;
      z = p[ii];
    }
  }
  cout << z << endl;

  return 0;
}