Problem p02641 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define fin                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define ii pair<int, int>
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fore(i, a, b) for (int i = a; i < b; i += 1)
#define forr(i, a) for (int i = a; i >= 0; i--)
#define fori(i, m) for (auto i = m.begin(); i != m.end(); i++)
#define w(t) while (t--)
#define sz(s) int(s.size())
#define cls(a, car) memset(a, car, sizeof(a))
#define db(x) cerr << #x << " is " << x << '\n'
#define FL freopen("in", "r", stdin), freopen("out", "w", stdout);
#define EjecuteTime                                                            \
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"
#define angle(x) double(x * acos(-1) / 180.0)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ii> vii;
const int N = 2 * 1e5 + 5;
const ll mod = 1e9 + 7;
const double E = 1e-9;
// int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
// int dx[] = {2, 1, -1, -2, -2, -1, 1, 2}, dy[] = {1, 2, 2, 1, -1, -2, -2, -1};
//  fflush(stdout)
//  cout << flush
//  a + b = a ^ b + 2 * (a & b)
int occ[105];
int main() {
  // FL;
  fin;
  int x, n;
  cin >> x >> n;
  vi v;
  fore(i, 0, n) {
    int y;
    cin >> y;
    occ[y] = 1;
  }
  fore(i, 1, 101) if (occ[i] == 0) v.pb(i);
  int a = 0, b = sz(v), mid;

  while (b - a > 1) {
    mid = (a + b) >> 1;
    if (v[mid] <= x)
      a = mid;
    else
      b = mid;
  }
  int val1 = v[a];
  a = -1;
  b = sz(v) - 1;
  while (b - a > 1) {
    mid = (a + b) >> 1;
    if (v[mid] >= x)
      b = mid;
    else
      a = mid;
  }
  int val2 = v[b];
  if (abs(val1 - x) == abs(val2 - x))
    cout << min(val1, val2) << '\n';
  else {
    if (abs(val1 - x) < abs(val2 - x))
      cout << val1 << '\n';
    else
      cout << val2 << '\n';
  }
  EjecuteTime;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define fin                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define ii pair<int, int>
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fore(i, a, b) for (int i = a; i < b; i += 1)
#define forr(i, a) for (int i = a; i >= 0; i--)
#define fori(i, m) for (auto i = m.begin(); i != m.end(); i++)
#define w(t) while (t--)
#define sz(s) int(s.size())
#define cls(a, car) memset(a, car, sizeof(a))
#define db(x) cerr << #x << " is " << x << '\n'
#define FL freopen("in", "r", stdin), freopen("out", "w", stdout);
#define EjecuteTime                                                            \
  cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"
#define angle(x) double(x * acos(-1) / 180.0)
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ii> vii;
const int N = 2 * 1e5 + 5;
const ll mod = 1e9 + 7;
const double E = 1e-9;
// int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};
// int dx[] = {2, 1, -1, -2, -2, -1, 1, 2}, dy[] = {1, 2, 2, 1, -1, -2, -2, -1};
//  fflush(stdout)
//  cout << flush
//  a + b = a ^ b + 2 * (a & b)
int occ[105];
int main() {
  // FL;
  fin;
  int x, n;
  cin >> x >> n;
  vi v;
  fore(i, 0, n) {
    int y;
    cin >> y;
    occ[y] = 1;
  }
  fore(i, 0, 105) if (occ[i] == 0) v.pb(i);
  int a = 0, b = sz(v), mid;

  while (b - a > 1) {
    mid = (a + b) >> 1;
    if (v[mid] <= x)
      a = mid;
    else
      b = mid;
  }
  int val1 = v[a];
  a = -1;
  b = sz(v) - 1;
  while (b - a > 1) {
    mid = (a + b) >> 1;
    if (v[mid] >= x)
      b = mid;
    else
      a = mid;
  }
  int val2 = v[b];
  if (abs(val1 - x) == abs(val2 - x))
    cout << min(val1, val2) << '\n';
  else {
    if (abs(val1 - x) < abs(val2 - x))
      cout << val1 << '\n';
    else
      cout << val2 << '\n';
  }
  EjecuteTime;
  return 0;
}