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

#include <bits/stdc++.h>
/**CLICK ss---> ctrl+j**/
#define PI 2.0 * acos(0.0)
#define ll long long int
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a) * (b)) / gcd(a, b)
#define MOD 1000000007
#define mx 123456789
using namespace std;
int inArr[10000];
void solve() {
  int tar, n;
  cin >> tar >> n;
  int mxx = 0;
  for (int i = 1; i <= n; i++) {
    int t;
    cin >> t;
    mxx = max(mxx, t);
    inArr[t] = 1;
  }
  int l = 0, r = mxx + 1;
  for (int i = tar; i >= 0; i--) {
    if (!inArr[i]) {
      l = i;
      break;
    }
  }
  for (int i = tar; i <= mxx + 1; i++) {
    if (!inArr[i]) {
      r = i;
      break;
    }
  }
  cout << (abs(l - tar) <= abs(r - tar) ? l : r) << endl;
  /*sort(a,a+m);
  /*vector<int>vec, vec1;
  for(i=0; i<m; i++)
  {
      if(a[i]<n)
          vec.push_back(a[i]);
      else if(a[i]>n)
          vec1.push_back(a[i]);
  }
  ll p=0, pp=0;
  /*for(i=0; i<m; i++)
  {
      cout<<a[i]<<" ";
  }
  cout<<endl;
  for(i=0; i<vec.size(); i++)
      cout<<vec[i]<<" ";
  cout<<endl;
  for(i=0; i<vec1.size(); i++)
  {
      cout<<vec1[i]<<" ";
  }
  cout<<endl;
  for(i=n-1; ; i--)
  {
      ll x=i;
      bool ok=true;
      for(j=0; j<vec.size(); j++)
      {
          if(vec[j]==x)
          {
              ok=false;
              break;
          }
      }
      if(ok==true)
      {
          p=x;
          break;
      }
  }
  for(i=n+1; ; i++)
  {
      ll x=i;
      bool ok=true;
      for(j=0; j<vec1.size(); j++)
      {
          if(vec1[j]==x)
          {
              ok=false;
              break;
          }
      }
      if(ok==true)
      {
          pp=x;
          break;
      }
  }
  if(p<0)
  {
      cout<<pp<<endl;
      return;
  }
  ll dif=abs(n-p);
  ll dif1=abs(n-pp);
  if(dif<dif1)
      cout<<p<<endl;
  else if(dif>dif1)
      cout<<pp<<endl;
 else
  cout<<min(p,pp)<<endl;
  ll pos=-1;
 for(i=0; i<m; i++)
 {
     if(a[i]==n)
     {
         pos=i;
         break;
     }
 }
 if(pos==-1)
 {
     for(i=0; i<m; i++)
     {
         if(a[i]>n)
         {
             pos=i-1;
             break;
         }
     }
 }
 ll p1=0;
 for(i=n-1; ;i--)
 {
     ll x=i;
     bool ok=true;
     for(j=pos; j>=0; j--)
     {
         if(a[j]==x)
         {
             ok=false;
             break;
         }
     }
     if(ok==true)
     {
         p1=x;
         break;
     }
 }



 ll pos1=-1;
 for(i=0; i<m; i++)
 {
     if(a[i]==n)
     {
         pos1=i;
         break;
     }
 }
 if(pos1==-1)
 {
     for(i=0; i<m; i++)
     {
         if(a[i]>n)
         {
             pos1=i-1;
             break;
         }
     }
 }
 ll p1=0;
 for(i=n-1; ;i--)
 {
     ll x=i;
     bool ok=true;
     for(j=pos; j>=0; j--)
     {
         if(a[j]==x)
         {
             ok=false;
             break;
         }
     }
     if(ok==true)
     {
         p1=x;
         break;
     }
 }*/
}
int main() {
  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    solve();
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
/**CLICK ss---> ctrl+j**/
#define PI 2.0 * acos(0.0)
#define ll long long int
#define gcd(a, b) __gcd(a, b)
#define lcm(a, b) ((a) * (b)) / gcd(a, b)
#define MOD 1000000007
#define mx 123456789
using namespace std;
void solve() {
  int n, m, i, j, k, l;
  cin >> m >> n;
  /*if(m==0)
  {
      cout<<n<<endl;
      return;
  }*/
  int a[n + 2];
  for (i = 0; i < n; i++) {
    int x;
    cin >> x;
    a[x] = 1;
  }
  /*sort(a,a+m);
  /*vector<int>vec, vec1;
  for(i=0; i<m; i++)
  {
      if(a[i]<n)
          vec.push_back(a[i]);
      else if(a[i]>n)
          vec1.push_back(a[i]);
  }
  ll p=0, pp=0;
  /*for(i=0; i<m; i++)
  {
      cout<<a[i]<<" ";
  }
  cout<<endl;
  for(i=0; i<vec.size(); i++)
      cout<<vec[i]<<" ";
  cout<<endl;
  for(i=0; i<vec1.size(); i++)
  {
      cout<<vec1[i]<<" ";
  }
  cout<<endl;
  for(i=n-1; ; i--)
  {
      ll x=i;
      bool ok=true;
      for(j=0; j<vec.size(); j++)
      {
          if(vec[j]==x)
          {
              ok=false;
              break;
          }
      }
      if(ok==true)
      {
          p=x;
          break;
      }
  }
  for(i=n+1; ; i++)
  {
      ll x=i;
      bool ok=true;
      for(j=0; j<vec1.size(); j++)
      {
          if(vec1[j]==x)
          {
              ok=false;
              break;
          }
      }
      if(ok==true)
      {
          pp=x;
          break;
      }
  }
  if(p<0)
  {
      cout<<pp<<endl;
      return;
  }
  ll dif=abs(n-p);
  ll dif1=abs(n-pp);
  if(dif<dif1)
      cout<<p<<endl;
  else if(dif>dif1)
      cout<<pp<<endl;
 else
  cout<<min(p,pp)<<endl;
  ll pos=-1;
 for(i=0; i<m; i++)
 {
     if(a[i]==n)
     {
         pos=i;
         break;
     }
 }
 if(pos==-1)
 {
     for(i=0; i<m; i++)
     {
         if(a[i]>n)
         {
             pos=i-1;
             break;
         }
     }
 }
 ll p1=0;
 for(i=n-1; ;i--)
 {
     ll x=i;
     bool ok=true;
     for(j=pos; j>=0; j--)
     {
         if(a[j]==x)
         {
             ok=false;
             break;
         }
     }
     if(ok==true)
     {
         p1=x;
         break;
     }
 }



 ll pos1=-1;
 for(i=0; i<m; i++)
 {
     if(a[i]==n)
     {
         pos1=i;
         break;
     }
 }
 if(pos1==-1)
 {
     for(i=0; i<m; i++)
     {
         if(a[i]>n)
         {
             pos1=i-1;
             break;
         }
     }
 }
 ll p1=0;
 for(i=n-1; ;i--)
 {
     ll x=i;
     bool ok=true;
     for(j=pos; j>=0; j--)
     {
         if(a[j]==x)
         {
             ok=false;
             break;
         }
     }
     if(ok==true)
     {
         p1=x;
         break;
     }
 }*/

  int minn = 110, now = 110;
  for (int i = 105; i >= 0; i--) {
    if (!a[i] && (fabs(i - m) < minn || fabs(i - m) == minn && i < now)) {
      minn = fabs(i - m);
      now = i;
    }
  }
  cout << now << endl;
}
int main() {
  int t;
  // cin>>t;
  t = 1;
  while (t--) {
    solve();
  }
  return 0;
}