Problem p02641 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
static int fast_io = []() {
  std::ios::sync_with_stdio(false);
  cin.tie(nullptr);
  return 0;
}();
int BinarySearch(vector<int> tab, int target) {
  int l = 0, r = tab.size() - 1, mid;
  while (l <= r) {
    mid = l + ((r - l) / 2);
    if (tab[mid] == target) {
      return mid;
    } else if (tab[mid] < target) {
      l = mid + 1;
    } else // tab[mid] > target)
    {
      r = mid - 1;
    }
  }
  return -1;
}
int main() {
  int x, n;
  cin >> x >> n;
  vector<int> tab(n);
  for (int i = 0; i < n; i++) {
    cin >> tab[i];
  }
  sort(tab.begin(), tab.end());
  int index = BinarySearch(tab, x);
  if (index == -1) {
    cout << x << "\\n";
    return 0;
  } else {
    int previusLeft = tab[index];
    int previusRight = tab[index];
    int leftPointer = index - 1;
    int rightPointer = index + 1;
    bool found = false;
    while (!found && (leftPointer != -1 || rightPointer != n)) {
      if (leftPointer >= 0) {
        if (tab[leftPointer] != previusLeft - 1) {
          cout << previusLeft - 1 << "\\n";
          return 0;
        } else {
          previusLeft = tab[leftPointer];
          leftPointer--;
        }
      }
      if (rightPointer < n && !found) {
        if (tab[rightPointer] != previusRight + 1) {
          cout << previusRight + 1 << "\\n";
          return 0;
        } else {
          previusRight = tab[rightPointer];
          rightPointer++;
        }
      }
    }
    if (abs(x - (previusLeft - 1)) <= abs(previusRight + 1 - x)) {
      cout << previusLeft - 1 << "\\n";
    } else {
      cout << previusRight + 1 << "\\n";
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
static int fast_io = []() {
  std::ios::sync_with_stdio(false);
  cin.tie(nullptr);
  return 0;
}();
int BinarySearch(vector<int> tab, int target) {
  int l = 0, r = tab.size() - 1, mid;
  while (l <= r) {
    mid = l + ((r - l) / 2);
    if (tab[mid] == target) {
      return mid;
    } else if (tab[mid] < target) {
      l = mid + 1;
    } else // tab[mid] > target)
    {
      r = mid - 1;
    }
  }
  return -1;
}
int main() {
  int x, n;
  cin >> x >> n;
  vector<int> tab(n);
  for (int i = 0; i < n; i++) {
    cin >> tab[i];
  }
  sort(tab.begin(), tab.end());
  int index = BinarySearch(tab, x);
  if (index == -1) {
    cout << x << "\\n";
  } else {
    int previusLeft = tab[index];
    int previusRight = tab[index];
    int leftPointer = index - 1;
    int rightPointer = index + 1;
    int answer = 0;
    bool found = false;
    while (!found) {
      if (leftPointer >= 0) {
        if (tab[leftPointer] != previusLeft - 1) {
          found = true;
          answer = previusLeft - 1;
        } else {
          previusLeft = tab[leftPointer];
          leftPointer--;
        }
      }
      if (rightPointer < n && !found) {
        if (tab[rightPointer] != previusRight + 1) {
          found = true;
          answer = previusRight + 1;
        } else {
          previusRight = tab[rightPointer];
          rightPointer++;
        }
      }
    }
    cout << answer << "\\n";
  }

  return 0;
}