Problem p03611 (C++)

Buggy submission (status: Runtime Error):
#ifdef LOCAL
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#else
#include <bits/stdc++.h>
#endif
using namespace std;

typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
#define _sort(arg) sort(begin(arg), end(arg))
#define MOD 1000000007
#define pb push_back
#define DEBUG(x) cout << #x << ": " << x << endl;

/*
__attribute__((constructor))
void initial()
{
        cin.tie(NULL);
        ios::sync_with_stdio(false);
}
*/

int main() {
  cin.tie(NULL);
  ios::sync_with_stdio(false);

  int n;

  cin >> n;

  int a[n];
  // map<int, int> m;
  int c[100010];
  memset(c, 0, sizeof(c));

  rep(i, n) {
    cin >> a[i];
    // map[a[i]]++;
    c[a[i]]++;
    c[a[i] - 1]++;
    c[a[i] + 1]++;
  }

  // sort(a, a+n);
  /*
  map<int, int> m;

  rep(i, n)
  {
          if (i == 0)
          {
                  m[a[i]] = 0;
          }
          else
          {
                  if (m[a[i]] == 0)
                  {
                          m[a[i]] = i;
                  }
          }
  }

  map<int, int>::iterator it = map.begin();
  int max_s = 0;

  for(int i = 0; i < n; i++)
  {
          int next_a = a[i] + 1;
          int next_na  = a[i] + 2;

          if (m.find(next_na) != m.end())
  }
  */

  /*
  int max_s = 0;

  for(int i = 0; i < n; i++)
  {
          int next_a = a[i] + 1;
          int next_na  = a[i] + 2;

          if (m.find(next_na) != m.end())
          {

          }
  }
  */

  int max_s = 0;
  for (int i = 0; i < 100000; i++) {
    if (c[i] > max_s) {
      max_s = c[i];
    }
  }

  cout << max_s << endl;

  return 0;
}


Fixed submission:
#ifdef LOCAL
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <vector>
#else
#include <bits/stdc++.h>
#endif
using namespace std;

typedef long long ll;
#define rep(i, n) for (int i = 0; i < n; i++)
#define _sort(arg) sort(begin(arg), end(arg))
#define MOD 1000000007
#define pb push_back
#define DEBUG(x) cout << #x << ": " << x << endl;

/*
__attribute__((constructor))
void initial()
{
        cin.tie(NULL);
        ios::sync_with_stdio(false);
}
*/

int main() {
  cin.tie(NULL);
  ios::sync_with_stdio(false);

  int n;

  cin >> n;

  int a[n];
  // map<int, int> m;
  int c[100010];
  memset(c, 0, sizeof(c));

  rep(i, n) {
    cin >> a[i];
    // map[a[i]]++;
    c[a[i]]++;
    if (a[i] > 0) {
      c[a[i] - 1]++;
    }

    c[a[i] + 1]++;
  }

  // sort(a, a+n);
  /*
  map<int, int> m;

  rep(i, n)
  {
          if (i == 0)
          {
                  m[a[i]] = 0;
          }
          else
          {
                  if (m[a[i]] == 0)
                  {
                          m[a[i]] = i;
                  }
          }
  }

  map<int, int>::iterator it = map.begin();
  int max_s = 0;

  for(int i = 0; i < n; i++)
  {
          int next_a = a[i] + 1;
          int next_na  = a[i] + 2;

          if (m.find(next_na) != m.end())
  }
  */

  /*
  int max_s = 0;

  for(int i = 0; i < n; i++)
  {
          int next_a = a[i] + 1;
          int next_na  = a[i] + 2;

          if (m.find(next_na) != m.end())
          {

          }
  }
  */

  int max_s = 0;
  for (int i = 0; i < 100000; i++) {
    if (c[i] > max_s) {
      max_s = c[i];
    }
  }

  cout << max_s << endl;

  return 0;
}