Problem p03611 (C++)

Buggy submission (status: Runtime Error):
/* tempate
 *
 *
 */

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int(i) = (n); (i) >= (0); (i)--)
#define SIZE(x) ((int)(x).size())
#define GCD(a, b)                                                              \
  int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
#define QP(a, b)                                                               \
  int qp(int a, ll b) {                                                        \
    int ans = 1;                                                               \
    do {                                                                       \
      if (b & 1)                                                               \
        ans = 1ll * ans * a % mo;                                              \
      a = 1ll * a * a % mo;                                                    \
    } while (b >>= 1);                                                         \
    return ans;                                                                \
  }
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define all(v) (v).begin(), (v).end()

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }
template <typename A, size_t N, typename T>
void FILL(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

typedef vector<int, int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1LL << 60;
const ll MOD = 1000000007LL;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> neighbors(1e5 + 1);
  rep(i, n) cin >> a[i];
  for (ll i = 0; i < n; i++) {
    neighbors[a[i] - 1]++;
    neighbors[a[i]]++;
    neighbors[a[i] + 1]++;
  }
  sort(all(neighbors), greater<ll>());
  cout << neighbors[0];
  cout << endl;
  return 0;
}


Fixed submission:
/* tempate
 *
 *
 */

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int(i) = (n); (i) >= (0); (i)--)
#define SIZE(x) ((int)(x).size())
#define GCD(a, b)                                                              \
  int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
#define QP(a, b)                                                               \
  int qp(int a, ll b) {                                                        \
    int ans = 1;                                                               \
    do {                                                                       \
      if (b & 1)                                                               \
        ans = 1ll * ans * a % mo;                                              \
      a = 1ll * a * a % mo;                                                    \
    } while (b >>= 1);                                                         \
    return ans;                                                                \
  }
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define all(v) (v).begin(), (v).end()

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }
template <typename A, size_t N, typename T>
void FILL(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

typedef vector<int, int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1LL << 60;
const ll MOD = 1000000007LL;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  vector<ll> a(n);
  vector<ll> neighbors(1e5 + 1);
  rep(i, n) cin >> a[i];
  for (ll i = 0; i < n; i++) {
    if (a[i] != 0) {
      neighbors[a[i] - 1]++;
    }
    neighbors[a[i]]++;
    neighbors[a[i] + 1]++;
  }
  sort(all(neighbors), greater<ll>());
  cout << neighbors[0];
  cout << endl;
  return 0;
}