Problem p02958 (C++)

Buggy submission (status: Runtime Error):
/*~~~~~~~~~~~~~~~~~~~~~~
  Code by gkothyari
~~~~~~~~~~~~~~~~~~~~~~~*/
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define foreach(it, c)                                                         \
  for (typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define endl "\n"
#define pb push_back
#define eb emplace_back
#define ll long long
#define ld long double
#define F first
#define S second
#define mp make_pair
#define clr(x) x.clear()
#define MAX 1e9
#define MIN -1e9
#define vi vector<int>
#define vvi vector<vi>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define max(x, y) (x > y) ? x : y
#define min(x, y) (x < y) ? x : y
#define mid(s, e) (s + (e - s) / 2)
#define mini INT_MIN
#define sqr(x) ((x) * (x))
#define ones(x) __builtin_popcount(x)
#define onesll(x) __builtin_popcountll(x)
#define PI acos(-1.0)
typedef vector<pii> vpii;
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
ll power(ll a, ll n) {
  ll p = 1;
  while (n > 0) {
    if (n % 2) {
      p = p * a;
    }
    n >>= 1;
    a *= a;
  }
  return p;
}
ll power(ll a, ll n, ll mod) {
  ll p = 1;
  while (n > 0) {
    if (n % 2) {
      p = p * a;
      p %= mod;
    }
    n >>= 1;
    a *= a;
    a %= mod;
  }
  return p % mod;
}

int main() {
  cout << fixed << setprecision(15);
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  ll n, i, count = 0;
  vector<int> v;
  cin >> n;
  int a[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
    v.push_back(a[i]);
  }
  sort(v.begin(), v.end());
  for (i = 0; i < n; i++) {
    if (a[i] != v[i])
      count++;
  }
  if (count <= 2)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;

  return 0;
}


Fixed submission:
/*~~~~~~~~~~~~~~~~~~~~~~
  Code by gkothyari
~~~~~~~~~~~~~~~~~~~~~~~*/
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (a); i >= (b); i--)
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define foreach(it, c)                                                         \
  for (typeof((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define endl "\n"
#define pb push_back
#define eb emplace_back
#define ll long long
#define ld long double
#define F first
#define S second
#define mp make_pair
#define clr(x) x.clear()
#define MAX 1e9
#define MIN -1e9
#define vi vector<int>
#define vvi vector<vi>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define all(v) v.begin(), v.end()
#define max(x, y) (x > y) ? x : y
#define min(x, y) (x < y) ? x : y
#define mid(s, e) (s + (e - s) / 2)
#define mini INT_MIN
#define sqr(x) ((x) * (x))
#define ones(x) __builtin_popcount(x)
#define onesll(x) __builtin_popcountll(x)
#define PI acos(-1.0)
typedef vector<pii> vpii;
const int INF = 0x3f3f3f3f, MOD = 1e9 + 7;
ll power(ll a, ll n) {
  ll p = 1;
  while (n > 0) {
    if (n % 2) {
      p = p * a;
    }
    n >>= 1;
    a *= a;
  }
  return p;
}
ll power(ll a, ll n, ll mod) {
  ll p = 1;
  while (n > 0) {
    if (n % 2) {
      p = p * a;
      p %= mod;
    }
    n >>= 1;
    a *= a;
    a %= mod;
  }
  return p % mod;
}

int main() {
  cout << fixed << setprecision(15);
  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #endif
  ios_base::sync_with_stdio(false);
  cin.tie(0);

  ll n, i, count = 0;
  vector<int> v;
  cin >> n;
  int a[n];
  for (i = 0; i < n; i++) {
    cin >> a[i];
    v.push_back(a[i]);
  }
  sort(v.begin(), v.end());
  for (i = 0; i < n; i++) {
    if (a[i] != v[i])
      count++;
  }
  if (count <= 2)
    cout << "YES" << endl;
  else
    cout << "NO" << endl;

  return 0;
}