Language: C++


#include<iostream>
#include<algorithm>
#include<cmath>
#include<map>
int main() {
  int x,n;
  cin >> x >> n;
  if (!n)
  {
    cout << x << endl;
    return 0;
  }
  if(n==0){
		cout<<x;
		return 0;
	}
  vector<int> v(n);
  for (int i = 0; i < n; ++i)
    cin >> v[i];
  sort(v.begin(), v.end());
  int start = 0, end = n - 1;
  if (x < v[start] || x>v[end])
  {
    cout << x << endl;
    return 0;
  }
  if (x == v[start])
  {q
    cout << (v[start]-1)<< endl;
    return 0;
  }
  int i;
  for (i = 0; i < n; ++i)
    if (v[i] == x)
      break;
  if (i == n)
  {
    cout << x << endl;
    return 0;
  }
  int j = i, k = i;
  while (j >= 0 && k <= end)
  {
    if (j-1>=0 &&  abs(v[j] - v[j -1]) > 1)
    {
      cout << (v[j] - 1);
      return 0;
    }
    if (k+1<=end &&  abs(v[k] - v[k + 1]) > 1)
    {
      cout << (v[k] + 1);
      return 0;
    }
    --j;
    ++k;
  }
  if (abs(x - (v[0] - 1)) > abs(x - (v[end] + 1)))
  {
    cout << (v[end] + 1);
    return 0;
  }
  cout << (v[0] - 1) << endl;
  return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:8:3: error: 'cin' was not declared in this scope; did you mean 'std::cin'?
    8 |   cin >> x >> n;
      |   ^~~
      |   std::cin
In file included from a.cc:2:
/usr/include/c++/14/iostream:62:18: note: 'std::cin' declared here
   62 |   extern istream cin;           ///< Linked to standard input
      |                  ^~~
a.cc:11:5: error: 'cout' was not declared in this scope; did you mean 'std::cout'?
   11 |     cout << x << endl;
      |     ^~~~
      |     std::cout
/usr/include/c++/14/iostream:63:18: note: 'std::cout' declared here
   63 |   extern ostream cout;          ///< Linked to standard output
      |                  ^~~~
a.cc:11:18: error: 'endl' was not declared in this scope; did you mean 'std::endl'?
   11 |     cout << x << endl;
      |                  ^~~~
      |                  std::endl
In file included from /usr/include/c++/14/iostream:41:
/usr/include/c++/14/ostream:744:5: note: 'std::endl' declared here
  744 |     endl(basic_ostream<_CharT, _Traits>& __os)
      |     ^~~~
a.cc:15:17: error: 'cout' was not declared in this scope; did you mean 'std::cout'?
   15 |                 cout<<x;
      |                 ^~~~
      |                 std::cout
/usr/include/c++/14/iostream:63:18: note: 'std::cout' declared here
   63 |   extern ostream cout;          ///< Linked to standard output
      |                  ^~~~
a.cc:18:3: error: 'vector' was not declared in this scope
   18 |   vector<int> v(n);
      |   ^~~~~~
a.cc:18:10: error: expected primary-expression before 'int'
   18 |   vector<int> v(n);
      |          ^~~
a.cc:20:12: error: 'v' was not declared in this scope
   20 |     cin >> v[i];
      |            ^
a.cc:21:8: error: 'v' was not declared in this scope
   21 |   sort(v.begin(), v.end());
      |        ^
a.cc:21:3: error: 'sort' was not declared in this scope; did you mean 'std::sort'?
   21 |   sort(v.begin(), v.end());
      |   ^~~~
      |   std::sort
In file included from /usr/include/c++/14/algorithm:86,
                 from a.cc:3:
/usr/include/c++/14/pstl/glue_algorithm_defs.h:296:1: note: 'std::sort' declared here
  296 | sort(_ExecutionPolicy&& __exec, _RandomAccessIterator __first, _RandomAccessIterator __last);
      | ^~~~
a.cc:25:5: error: 'cout' was not declared in this scope; did you mean 'std::cout'?
   25 |     cout << x << endl;
      |     ^~~~
      |     std::cout
/usr/include/c++/14/iostream:63:18: note: 'std::cout' declared here
   63 |   extern ostream cout;          ///< Linked to standard output
      |                  ^~~~
a.cc:25:18: error: 'endl' was not declared in this scope; did you mean 'std::endl'?
   25 |     cout << x << endl;
      |                  ^~~~
      |                  std::endl
/usr/include/c++/14/ostream:744:5: note: 'std::endl' declared here
  744 |     endl(basic_ostream<_CharT, _Traits>& __os)
      |     ^~~~
a.cc:29:4: error: 'q' was not declared in this scope
   29 |   {q
      |    ^
a.cc:39:5: error: 'cout' was not declared in this scope; did you mean 'std::cout'?
   39 |     cout << x << endl;
      |     ^~~~
      |     std::cout
/usr/include/c++/14/iostream:63:18: note: 'std::cout' declared here
   63 |   extern ostream cout;          ///< Linked to standard output
      |                  ^~~~
a.cc:39:18: error: 'endl' was not declared in this scope; did you mean 'std::endl'?
   39 |     cout << x << endl;
      |                  ^~~~
      |                  std::endl
/usr/include/c++/14/ostream:744:5: note: 'std::endl' declared here
  744 |     endl(basic_ostream<_CharT, _Traits>& __os)
      |     ^~~~
a.cc:47:7: error: 'cout' was not declared in this scope; did you mean 'std::cout'?
   47 |       cout << (v[j] - 1);
      |       ^~~~
      |       std::cout
/usr/include/c++/14/iostream:63:18: note: 'std::cout' declared here
   63 |   extern ostream cout;          ///< Linked to standard output
      |                  ^~~~
a.cc:52:7: error: 'cout' was not declared in this scope; did you mean 'std::cout'?
   52 |       cout << (v[k] + 1);
      |       ^~~~
      |       std::cout
/usr/include/c++/14/iostream:63:18: note: 'std::cout' declared here
   63 |   extern ostream cout;          ///< Linked to standard output
      |                  ^~~~
a.cc:60:5: error: 'cout' was not declared in this scope; did you mean 'std::cout'?
   60 |     cout << (v[end] + 1);
      |     ^~~~
      |     std::cout
/usr/include/c++/14/iostream:63:18: note: 'std::cout' declared here
   63 |   extern ostream cout;          ///< Linked to standard output
      |                  ^~~~
a.cc:63:3: error: 'cout' was not declared in this scope; did you mean 'std::cout'?
   63 |   cout << (v[0] - 1) << endl;
      |   ^~~~
      |   std::cout
/usr/include/c++/14/iostream:63:18: note: 'std::cout' declared here
   63 |   extern ostream cout;          ///< Linked to standard output
      |                  ^~~~
a.cc:63:25: error: 'endl' was not declared in this scope; did you mean 'std::endl'?
   63 |   cout << (v[0] - 1) << endl;
      |                         ^~~~
      |                         std::endl
/usr/include/c++/14/ostream:744:5: note: 'std::endl' declared here
  744 |     endl(basic_ostream<_CharT, _Traits>& __os)
      |     ^~~~