Problem p03688 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pf push_front
#define iter(v, i)                                                             \
  for (__typeof__((v).begin()) i = (v).begin(); i != (v).end(); i++)
#define fast_io_without_cstdio ios_base::sync_with_stdio(false), cin.tie(NULL)
#define all(v) (v).begin(), (v).end()
#define rep(i, s, e) for (int i = s; i < e; i++)

#ifdef __linux__
#define gc getchar_unlocked
#define pc putchar_unlocked
#else
#define gc getchar
#define pc putchar
#endif

#if __cplusplus <= 199711L
template <class BidirIt>
BidirIt prev(BidirIt it,
             typename iterator_traits<BidirIt>::difference_type n = 1) {
  advance(it, -n);
  return it;
}

template <class ForwardIt>
ForwardIt next(ForwardIt it,
               typename iterator_traits<ForwardIt>::difference_type n = 1) {
  advance(it, n);
  return it;
}
#endif

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long double ldouble;

const double EPS = 1e-9;
const double PI = 3.141592653589793238462;

template <typename T> inline T sq(T a) { return a * a; }

// #ifdef LOCAL_MACHINE
// #endif

const int MAXN = 1e5 + 5;

int cnt[MAXN];

int main() {
  // freopen("", "r", stdin);
  // freopen("", "w", stdout);
  int n;
  scanf("%d", &n);
  int diff = 0, mn = 1e9, mx = 0;
  for (int i = 0; i < n; i++) {
    int a;
    scanf("%d", &a);
    cnt[a]++;
    if (cnt[a] == 1)
      diff++;
    mn = min(mn, a);
    mx = max(mx, a);
  }
  if (mx == mn) {
    if (n == mx + 1)
      puts("Yes");
    else if (mx * 2 <= n)
      puts("Yes");
    else
      puts("No");
  } else if (mn + 1 == mx) {
    assert(false);
    if (cnt[mn] + 1 == mx && cnt[mx] > 1)
      puts("Yes");
    else
      puts("No");
  } else {
    puts("No");
  }
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pf push_front
#define iter(v, i)                                                             \
  for (__typeof__((v).begin()) i = (v).begin(); i != (v).end(); i++)
#define fast_io_without_cstdio ios_base::sync_with_stdio(false), cin.tie(NULL)
#define all(v) (v).begin(), (v).end()
#define rep(i, s, e) for (int i = s; i < e; i++)

#ifdef __linux__
#define gc getchar_unlocked
#define pc putchar_unlocked
#else
#define gc getchar
#define pc putchar
#endif

#if __cplusplus <= 199711L
template <class BidirIt>
BidirIt prev(BidirIt it,
             typename iterator_traits<BidirIt>::difference_type n = 1) {
  advance(it, -n);
  return it;
}

template <class ForwardIt>
ForwardIt next(ForwardIt it,
               typename iterator_traits<ForwardIt>::difference_type n = 1) {
  advance(it, n);
  return it;
}
#endif

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef long double ldouble;

const double EPS = 1e-9;
const double PI = 3.141592653589793238462;

template <typename T> inline T sq(T a) { return a * a; }

// #ifdef LOCAL_MACHINE
// #endif

const int MAXN = 1e5 + 5;

int cnt[MAXN];

int main() {
  // freopen("", "r", stdin);
  // freopen("", "w", stdout);
  int n;
  scanf("%d", &n);
  int diff = 0, mn = 1e9, mx = 0;
  for (int i = 0; i < n; i++) {
    int a;
    scanf("%d", &a);
    cnt[a]++;
    if (cnt[a] == 1)
      diff++;
    mn = min(mn, a);
    mx = max(mx, a);
  }
  if (mx == mn) {
    if (n == mx + 1)
      puts("Yes");
    else if (mx * 2 <= n)
      puts("Yes");
    else
      puts("No");
  } else if (mn + 1 == mx) {
    // assert(false);
    if (mx - cnt[mn] > 0 && (mx - cnt[mn]) * 2 <= cnt[mx])
      puts("Yes");
    else
      puts("No");
  } else {
    puts("No");
  }
  return 0;
}