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

#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;

#define all(arr) arr.begin(), arr.end()
#define f first
#define s second
#define debug1(x) cout << x << "\\n"
#define debug2(x, y) cout << x << " " << y << "\\n"
#define debug3(x, y, z) cout << x << " " << y << " " << z << "\\n"
#define nl cout << "\\n";
#define pq priority_queue
#define inf 0x3f3f3f3f
#define test cout << "abcd\\n";
#define pi pair<int, int>
#define pii pair<int, pi>
#define pb push_back
#define MOD 1000000007

template <typename T> void input(vector<T> &arr, lli n) {
  T temp;
  for (lli i = 0; i < n; i++)
    cin >> temp, arr.push_back(temp);
}

template <typename T> void output(vector<T> arr) {
  for (auto x : arr)
    cout << x << " ";
  cout << endl;
}

template <typename T> void input_set(set<T> &arr, lli n) {
  T temp;
  for (lli i = 0; i < n; i++)
    cin >> temp, arr.insert(temp);
}

lli mul(lli a, lli b) { return (a % MOD * b % MOD) % MOD; }

lli power(lli num, lli base) {
  lli ans = 1;
  while (base > 0) {
    if (base & 1)
      ans = mul(ans, num);

    num = mul(num, num);
    base >>= 1;
  }

  return ans;
}

void solve() {
  int n, x;
  cin >> x >> n;
  vector<int> arr;
  input(arr, n);
  sort(all(arr));
  int t1 = x;
  while (true) {
    int ind = lower_bound(all(arr), t1) - arr.begin();
    if (ind < 0 or ind == n or arr[ind] != t1)
      break;
    --t1;
  }

  int t2 = x;
  while (true) {
    int ind = lower_bound(all(arr), t2) - arr.begin();
    if (ind < 0 or ind == n or arr[ind] != t2)
      break;
    ++t2;
  }

  int mi = min(abs(x - t1), abs(x - t2));
  if (mi == abs(x - t1))
    cout << t1 << endl;
  else
    cout << t2 << endl;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  lli testcases = 1;
  while (testcases--) {
    solve();
  }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
typedef long long int lli;

#define all(arr) arr.begin(), arr.end()
#define f first
#define s second
#define debug1(x) cout << x << "\\n"
#define debug2(x, y) cout << x << " " << y << "\\n"
#define debug3(x, y, z) cout << x << " " << y << " " << z << "\\n"
#define nl cout << "\\n";
#define pq priority_queue
#define inf 0x3f3f3f3f
#define test cout << "abcd\\n";
#define pi pair<int, int>
#define pii pair<int, pi>
#define pb push_back
#define MOD 1000000007

template <typename T> void input(vector<T> &arr, lli n) {
  T temp;
  for (lli i = 0; i < n; i++)
    cin >> temp, arr.push_back(temp);
}

template <typename T> void output(vector<T> arr) {
  for (auto x : arr)
    cout << x << " ";
  cout << endl;
}

template <typename T> void input_set(set<T> &arr, lli n) {
  T temp;
  for (lli i = 0; i < n; i++)
    cin >> temp, arr.insert(temp);
}

lli mul(lli a, lli b) { return (a % MOD * b % MOD) % MOD; }

lli power(lli num, lli base) {
  lli ans = 1;
  while (base > 0) {
    if (base & 1)
      ans = mul(ans, num);

    num = mul(num, num);
    base >>= 1;
  }

  return ans;
}

void solve() {
  int n, x;
  cin >> x >> n;
  vector<int> arr;
  input(arr, n);
  sort(all(arr));
  int t1 = x;
  while (true) {
    int ind = lower_bound(all(arr), t1) - arr.begin();
    if (ind < 0 or arr[ind] != t1)
      break;
    --t1;
  }

  int t2 = x;
  while (true) {
    int ind = lower_bound(all(arr), t2) - arr.begin();
    if (ind == n or arr[ind] != t2)
      break;
    ++t2;
  }

  int mi = min(abs(x - t1), abs(x - t2));
  if (mi == abs(x - t1))
    cout << t1 << endl;
  else
    cout << t2 << endl;
}

int main() {

  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  lli testcases = 1;
  while (testcases--) {
    solve();
  }
}