prithivMLmods/Coder-Stat huggingface dataset:
Problem p02832 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <cassert>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
const int mod = 1000000007;
const int inf = 1ll << 61;
typedef pair<int, int> P;
typedef pair<P, int> PP;
int a[100006];
signed main() {
  int n;
  cin >> n;
  rep(i, n) cin >> a[i];
  int now = 0;
  rep(i, n) {
    if (a[i] == now + 1)
      now++;
  }
  if (now)
    cout << n - now << endl;
  else
    cout << -1 << endl;
}

Fixed submission:
#include "bits/stdc++.h"
#include <cassert>
using namespace std;
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
const int mod = 1000000007;
const int inf = 1ll << 61;
typedef pair<int, int> P;
typedef pair<P, int> PP;
int a[1000006];
signed main() {
  int n;
  cin >> n;
  rep(i, n) cin >> a[i];
  int now = 0;
  rep(i, n) {
    if (a[i] == now + 1)
      now++;
  }
  if (now)
    cout << n - now << endl;
  else
    cout << -1 << endl;
}
Problem p02832 (C++)

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

using namespace std;

int main() {
  int a, t = 1;
  long long b[20001];
  cin >> a;
  for (int i = 0; i < a; i++)
    cin >> b[i];
  for (int i = 0; i < a; i++)
    if (b[i] == t)
      t++;
  if (t > 1)
    cout << a - t + 1 << endl;
  else
    cout << "-1" << endl;
}

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

using namespace std;

int main() {
  int a, t = 1;
  long long b[200001];
  cin >> a;
  for (int i = 0; i < a; i++)
    cin >> b[i];
  for (int i = 0; i < a; i++)
    if (b[i] == t)
      t++;
  if (t > 1)
    cout << a - t + 1 << endl;
  else
    cout << "-1" << endl;
}
Problem p02832 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int break_num = 0;
  int a[n];
  bool break_flg[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    break_flg[i] = false;
  }

  for (int i = 0; i < n; i++) {
    int index_num = 0;
    for (int j = 0; j <= i; j++) {
      if (break_flg[j] == true) {
        continue;
      }
      if (break_flg[j] == false) {
        index_num++;
      }
      if (index_num != a[j]) {
        break_flg[j] = true;
        break_num++;
      }
    }
  }

  if (break_num == n) {
    cout << -1 << endl;
    ;
  } else {
    cout << break_num << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  int n;
  cin >> n;
  int break_num = 0;
  int a[n];
  bool break_flg[n];
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    break_flg[i] = false;
  }

  int index_num = 0;
  for (int i = 1; i <= n; i++) {
    if (i - break_num != a[i - 1]) {
      break_num++;
    }
  }

  if (break_num == n) {
    cout << -1 << endl;
    ;
  } else {
    cout << break_num << endl;
  }

  return 0;
}
Problem p02832 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

int main() {

  int length;
  cin >> length;

  int tot = 0;
  int brick[100000];
  int sen = 1;
  for (int i = 0; i < length; i++) {
    cin >> brick[i];
    if (brick[i] != sen) {
      tot++;
    } else {
      sen++;
    }
  }

  if (sen == 1) {
    tot = -1;
  }
  cout << tot << endl;

  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;

int main() {

  int length;
  cin >> length;

  int tot = 0;
  int brick[1000000];
  int sen = 1;
  for (int i = 0; i < length; i++) {
    cin >> brick[i];
    if (brick[i] != sen) {
      tot++;
    } else {
      sen++;
    }
  }

  if (sen == 1) {
    tot = -1;
  }
  cout << tot << endl;

  return 0;
}
Problem p02832 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
long long int a, b, c, ar[100009], sum, cnt;
int main() {
  cin >> a;
  long long int j = 1;

  for (int i = 1; i <= a; i++)
    cin >> ar[i];
  for (int i = 1; i <= a; i++) {
    if (ar[i] != j)
      cnt++;
    else {
      j++;
    }
  }

  if (cnt == a)
    cout << "-1" << endl;
  else
    cout << cnt << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
long long int a, b, c, ar[1000009], sum, cnt;
int main() {
  cin >> a;
  long long int j = 1;

  for (int i = 1; i <= a; i++)
    cin >> ar[i];
  for (int i = 1; i <= a; i++) {
    if (ar[i] != j)
      cnt++;
    else {
      j++;
    }
  }

  if (cnt == a)
    cout << "-1" << endl;
  else
    cout << cnt << endl;

  return 0;
}
Problem p02832 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
const int mod = 1000000007;
const int MOD = 998244353;
const ll INF = 1000000000000000000;

int A[100010];
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  int ans = 0;
  int num = 1;
  for (int i = 0; i < N; i++) {
    if (A[i] != num)
      ans++;
    else
      num++;
  }
  if (ans == N)
    cout << -1 << endl;
  else
    cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define ll long long
const int mod = 1000000007;
const int MOD = 998244353;
const ll INF = 1000000000000000000;

int A[200010];
int main() {
  int N;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> A[i];
  int ans = 0;
  int num = 1;
  for (int i = 0; i < N; i++) {
    if (A[i] != num)
      ans++;
    else
      num++;
  }
  if (ans == N)
    cout << -1 << endl;
  else
    cout << ans << endl;
}
Problem p02832 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

int main() {
  long N, a[20000];
  long i, k = 1;
  long count = 1;

  cin >> N;

  for (i = 1; i <= N; i++) {
    cin >> a[i];
  }

  for (i = 1; i <= N; i++)
    if (a[i] == count)
      count++;

  if (count == 1) {
    cout << "-1" << endl;
    return 0;
  }

  cout << N - count + 1 << endl;
  return 0;
}


Fixed submission:
#include <iostream>

using namespace std;

int main() {
  long N, a[200000];
  long i, k = 1;
  long count = 1;

  cin >> N;

  for (i = 1; i <= N; i++) {
    cin >> a[i];
  }

  for (i = 1; i <= N; i++)
    if (a[i] == count)
      count++;

  if (count == 1) {
    cout << "-1" << endl;
    return 0;
  }

  cout << N - count + 1 << endl;
  return 0;
}
Problem p02832 (C++)

Buggy submission (status: Runtime Error):
// in the name of god
// if you read this code please search about imam hussain
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define pb push_back
#define endl "\n"
#define X first
#define Y second
#define pii pair<int, int>
#define migmig                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define read                                                                   \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout)

const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9;
const int del = 728729;

ll poww(ll a, ll b, ll md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}
ll n;
ll q[maxn];
ll nw = 1;
ll ted;
int main() {
  migmig;
  vector<int> v;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> q[i];
  }

  for (int i = 0; i < n; i++) {
    if (q[i] == nw) {
      nw++;
    } else {
      ted++;
    }
  }
  if (nw == 1) {
    cout << "-1";
  } else {
    cout << ted;
  }
  return 0;
}


Fixed submission:
// in the name of god
// if you read this code please search about imam hussain
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define pb push_back
#define endl "\n"
#define X first
#define Y second
#define pii pair<int, int>
#define migmig                                                                 \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0)
#define read                                                                   \
  freopen("input.txt", "r", stdin);                                            \
  freopen("output.txt", "w", stdout)

const int maxn = 2e5 + 5;
const int mod = 1e9 + 7;
const int inf = 1e9;
const int del = 728729;

ll poww(ll a, ll b, ll md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}
ll n;
ll q[maxn];
ll nw = 1;
ll ted;
int main() {
  migmig;
  vector<int> v;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> q[i];
  }

  for (int i = 0; i < n; i++) {
    if (q[i] == nw) {
      nw++;
    } else {
      ted++;
    }
  }
  if (nw == 1) {
    cout << "-1";
  } else {
    cout << ted;
  }
  return 0;
}
Problem p02832 (C++)

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

#define REP(i, n) for (int i = 0; i < (int)(n); i++)

int gcd(int x, int y) {
  int t = 0;
  t = (x + y) / 2;
  if (x && y != t) {
    if (x > y)
      return gcd(x = x - x / y * y, y);
    else if (x < y)
      return gcd(x, y = y - y / x * x);
  } else if (x > y)
    return t = x;
  else
    return t = y;
}

int main() {
  int n;
  int a[n];
  int j = 0;
  bool ok = false;
  cin >> n;
  REP(i, n) { cin >> a[i]; }
  for (int i = 0; i < n; i++) {
    /* code */
    if (a[i] == 1 + j) {
      j++;
      ok = true;
    }
  }
  if (ok) {
    cout << n - j << endl;
  } else {
    cout << -1 << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

#define REP(i, n) for (int i = 0; i < (int)(n); i++)

int gcd(int x, int y) {
  int t = 0;
  t = (x + y) / 2;
  if (x && y != t) {
    if (x > y)
      return gcd(x = x - x / y * y, y);
    else if (x < y)
      return gcd(x, y = y - y / x * x);
  } else if (x > y)
    return t = x;
  else
    return t = y;
}

int main() {
  int n;
  int a[1000000];
  int j = 0;
  bool ok = false;
  cin >> n;
  REP(i, n) { cin >> a[i]; }
  for (int i = 0; i < n; i++) {
    /* code */
    if (a[i] == 1 + j) {
      j++;
      ok = true;
    }
  }
  if (ok) {
    cout << n - j << endl;
  } else {
    cout << -1 << endl;
  }
}
Problem p02832 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T> T gcd(T a, T b) {
  return ((a % b == 0) ? b : gcd(b, a % b));
}
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;

namespace Problem {
using namespace std;

class Solver {
public:
  int n;
  vector<int> a, r, memo;
  Solver(LL n) : n(n), a(n), r(n, -1), memo(200000, -1){};

  void solve() {
    bool ok = false;
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
      a[i]--;
      if (a[i] == 0)
        ok = true;
    }
    if (!ok) {
      cout << -1 << endl;
      return;
    }
    DBG(a)
    for (int i = n - 1; i >= 0; --i) {
      memo[a[i]] = i;
      if (memo[a[i] + 1] >= 0) {
        r[i] = memo[a[i] + 1];
      }
    }
    DBG(r)
    int p = 0;
    while (a[p] != 0)
      p++;
    int len = 0;
    DBG(p)
    while (p >= 0) {
      p = r[p];
      len++;
    }
    cout << n - len << endl;
  }
};

} // namespace Problem

int main() {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(12);
  long long n = 0;
  std::cin >> n;

  Problem::Solver sol(n);
  sol.solve();
  return 0;
}


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

#if MYDEBUG
#include "lib/cp_debug.hpp"
#else
#define DBG(...) ;
#endif
#if __cplusplus <= 201402L
template <typename T> T gcd(T a, T b) {
  return ((a % b == 0) ? b : gcd(b, a % b));
}
template <typename T> T lcm(T a, T b) { return a / gcd(a, b) * b; }
#endif
using LL = long long;
constexpr LL LINF = 334ll << 53;
constexpr int INF = 15 << 26;
constexpr LL MOD = 1E9 + 7;

namespace Problem {
using namespace std;

class Solver {
public:
  int n;
  vector<int> a, r, memo;
  Solver(LL n) : n(n), a(n), r(n, -1), memo(200001, -1){};

  void solve() {
    bool ok = false;
    for (int i = 0; i < n; ++i) {
      cin >> a[i];
      a[i]--;
      if (a[i] == 0)
        ok = true;
    }
    if (!ok) {
      cout << -1 << endl;
      return;
    }
    DBG(a)
    for (int i = n - 1; i >= 0; --i) {
      memo[a[i]] = i;
      if (memo[a[i] + 1] >= 0) {
        r[i] = memo[a[i] + 1];
      }
    }
    DBG(r)
    int p = 0;
    while (a[p] != 0)
      p++;
    int len = 0;
    DBG(p)
    while (p >= 0) {
      p = r[p];
      len++;
    }
    cout << n - len << endl;
  }
};

} // namespace Problem

int main() {
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(false);
  // std::cout << std::fixed << std::setprecision(12);
  long long n = 0;
  std::cin >> n;

  Problem::Solver sol(n);
  sol.solve();
  return 0;
}
Problem p02832 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>

int main() {
  int n;

  std::cin >> n;

  std::vector<int> a(n);
  for (int i = 0; i < n; i++) {
    std::cin >> a.at(i);
  }
  int ans = 0;
  int tmp = 1;
  int size = a.size();
  for (int i = 0; i < a.size(); i++) {
    if (a.at(i) != tmp) {
      a.erase(a.begin() + i);
      i--;
      ans++;
    } else {
      tmp++;
    }
  }
  if (ans == size) {
    ans = -1;
  }
  std::cout << ans << std::endl;

  return 0;
}


Fixed submission:
#include <iostream>
#include <vector>

int main() {
  int n;

  std::cin >> n;

  std::vector<int> a(n);
  for (int i = 0; i < n; i++) {
    std::cin >> a.at(i);
  }
  int ans = 0;
  int tmp = 1;
  int size = a.size();
  for (int i = 0; i < a.size(); i++) {
    if (a.at(i) != tmp) {
      ans++;
    } else {
      tmp++;
    }
  }
  if (ans == size) {
    ans = -1;
  }
  std::cout << ans << std::endl;

  return 0;
}
Problem p02832 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int N = 1e5 + 10;

int a[N];

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  int cnt = 1, ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == cnt)
      cnt++;
    else
      ans++;
  }
  if (ans == n)
    cout << -1 << endl;
  else
    cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

const int N = 5e5 + 10;

int a[N];

int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  int cnt = 1, ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] == cnt)
      cnt++;
    else
      ans++;
  }
  if (ans == n)
    cout << -1 << endl;
  else
    cout << ans << endl;
}
Problem p02832 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int n, f = 1, sum, a[1001];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    if (a[i] == f)
      f++;
    else
      sum++;
  if (f == 1)
    cout << -1;
  else
    cout << sum;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int n, f = 1, sum, a[200001];
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  for (int i = 1; i <= n; i++)
    if (a[i] == f)
      f++;
    else
      sum++;
  if (f == 1)
    cout << -1;
  else
    cout << sum;
}
Problem p02832 (C++)

Buggy submission (status: Runtime Error):
#define DBG 0 // submit 0
#define ll long long
#if DBG
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>
#else
#include <bits/stdc++.h>
#endif

using namespace std;

int main() {
  int a[100005], b[100005], c[100005];
  // bool flag=false,d[100005];
  int n, ans = 0;
  // char s[105],t[105],u;

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  // for(int i=0;i<n;i++) cin >> t[i];

  for (int i = 0; i < n; i++) {
    if (a[i] == ans + 1)
      ans++;
  }
  ans = n - ans;
  if (ans == n)
    ans = -1;

  // cout << (!flag ? "Yes" : "No");
  cout << ans;
  // cout << s;
  // printf("%.10f",ans_d);
  cout << endl;
}


Fixed submission:
#define DBG 0 // submit 0
#define ll long long
#if DBG
#include <algorithm>
#include <array>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>
#else
#include <bits/stdc++.h>
#endif

using namespace std;

int main() {
  int a[200005];
  // bool flag=false,d[100005];
  int n, ans = 0;
  // char s[105],t[105],u;

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  // for(int i=0;i<n;i++) cin >> t[i];

  for (int i = 0; i < n; i++) {
    if (a[i] == ans + 1)
      ans++;
  }
  ans = n - ans;
  if (ans == n)
    ans = -1;

  // cout << (!flag ? "Yes" : "No");
  cout << ans;
  // cout << s;
  // printf("%.10f",ans_d);
  cout << endl;
}
Problem p02832 (C++)

Buggy submission (status: Runtime Error):
/*
    Author: Nguyen Tan Bao
    Status:
    Idea:
*/

#include "bits/stdc++.h"
#define FI first
#define SE second
#define EPS 1e-9
#define ALL(a) a.begin(), a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORE(i, a, b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a)                                                          \
  for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x

using namespace std;
using ll = long long;
using ld = double;
typedef pair<int, int> II;
typedef pair<II, ll> III;
typedef complex<ld> cd;
typedef vector<cd> vcd;

const ll MODBASE = 1000000007LL;
const int MAXN = 100010;
const int MAXM = 1000;
const int MAXK = 16;
const int MAXQ = 200010;

int n, a[MAXN];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(nullptr);
  cin >> n;
  FOR(i, 1, n) cin >> a[i];
  int now = 1, res = 0;
  FOR(i, 1, n)
  if (a[i] == now) {
    now++;
  } else
    res++;
  if (now == 1)
    res = -1;
  cout << res;
  return 0;
}

Fixed submission:
/*
    Author: Nguyen Tan Bao
    Status:
    Idea:
*/

#include "bits/stdc++.h"
#define FI first
#define SE second
#define EPS 1e-9
#define ALL(a) a.begin(), a.end()
#define SZ(a) int((a).size())
#define MS(s, n) memset(s, n, sizeof(s))
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORE(i, a, b) for (int i = (a); i >= (b); i--)
#define FORALL(it, a)                                                          \
  for (__typeof((a).begin()) it = (a).begin(); it != (a).end(); it++)
//__builtin_ffs(x) return 1 + index of least significant 1-bit of x
//__builtin_clz(x) return number of leading zeros of x
//__builtin_ctz(x) return number of trailing zeros of x

using namespace std;
using ll = long long;
using ld = double;
typedef pair<int, int> II;
typedef pair<II, ll> III;
typedef complex<ld> cd;
typedef vector<cd> vcd;

const ll MODBASE = 1000000007LL;
const int MAXN = 200010;
const int MAXM = 1000;
const int MAXK = 16;
const int MAXQ = 200010;

int n, a[MAXN];

int main() {
  ios::sync_with_stdio(0);
  cin.tie(nullptr);
  cin >> n;
  FOR(i, 1, n) cin >> a[i];
  int now = 1, res = 0;
  FOR(i, 1, n)
  if (a[i] == now) {
    now++;
  } else
    res++;
  if (now == 1)
    res = -1;
  cout << res;
  return 0;
}
Problem p02832 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  int cnt = 0;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  int i = 0;

  while (i < n && a.at(i) != 0) {
    if (a.at(i) == i + 1) {
      i++;
      continue;
    }
    if (a.at(i) != i + 1) {
      a.erase(a.begin() + i);
      a.push_back(0);
      cnt++;
    }
  }
  if (cnt == n) {
    cout << "-1" << endl;
  } else {
    cout << cnt << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  cin >> n;
  int cnt = 0;
  vector<int> a(n);
  for (int i = 0; i < n; i++) {
    cin >> a.at(i);
  }
  int ans = 1;
  for (int i = 0; i < n; i++) {
    if (a.at(i) == ans) {
      ans++;
    } else {
      cnt++;
    }
  }
  if (cnt == n) {
    cout << "-1" << endl;
  } else {
    cout << cnt << endl;
  }
}
Problem p02832 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  int C = 0;
  int Size = N;
  int Num = 1;
  while (C < Size) {
    if (Num == A.at(C)) {
      Num++;
      C++;
    } else {
      A.erase(A.begin() + C);
      Size--;
    }
  }
  if (Num > 1) {
    cout << N - (Num - 1);
  } else {
    cout << -1;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  for (int i = 0; i < N; i++) {
    cin >> A.at(i);
  }
  int C = 0;
  int Size = N;
  int Num = 1;
  while (C < Size) {
    if (Num == A.at(C)) {
      Num++;
      C++;
    } else {
      C++;
    }
  }
  if (Num > 1) {
    cout << N - (Num - 1);
  } else {
    cout << -1;
  }
}
Problem p02832 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {

  int n, a[20010] = {}, serch = 1, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    if (a[i] == serch) {
      serch++;
    } else {
      ans++;
    }
  }

  if (serch == 1) {
    cout << "-1" << endl;
  } else {
    cout << ans << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
int main() {

  int n, a[200100] = {}, serch = 1, ans = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < n; i++) {
    if (a[i] == serch) {
      serch++;
    } else {
      ans++;
    }
  }

  if (serch == 1) {
    cout << "-1" << endl;
  } else {
    cout << ans << endl;
  }

  return 0;
}
Problem p02832 (C++)

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

int main() {
  int N;
  cin >> N;

  vector<int> a(N);
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
  }

  if (N == 1) {
    if (a[1] == 1) {
      cout << 0 << endl;
    } else {
      cout << -1 << endl;
    }
    return 0;
  }

  int num = 1, ans = 0;
  for (int i = 1; i <= N; i++) {
    if (a[i] == num) {
      num++;
    } else {
      ans++;
    }
  }

  if (ans == N) {
    cout << -1 << endl;
  } else if (num > 1) {
    cout << ans << endl;
  } else {
    cout << -1 << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int N;
  cin >> N;

  int a[N];
  for (int i = 1; i <= N; i++) {
    cin >> a[i];
  }

  if (N == 1) {
    if (a[1] == 1) {
      cout << 0 << endl;
    } else {
      cout << -1 << endl;
    }
    return 0;
  }

  int num = 1, ans = 0;
  for (int i = 1; i <= N; i++) {
    if (a[i] == num) {
      num++;
    } else {
      ans++;
    }
  }

  if (ans == N) {
    cout << -1 << endl;
  } else if (num > 1) {
    cout << ans << endl;
  } else {
    cout << -1 << endl;
  }

  return 0;
}
Problem p02832 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define int long long int
mt19937 rng(std::chrono::duration_cast<std::chrono::nanoseconds>(
                chrono::high_resolution_clock::now().time_since_epoch())
                .count());
#define mp make_pair
#define pb push_back
#define F first
#define S second
const int N = 200005;
#define M 1000000007
#define double long double
#define BINF 10000000000000000
#define init(arr, val) memset(arr, val, sizeof(arr))
#define MAXN 15000001
#define deb(x) cout << #x << " " << x << "\n";
const int LG = 22;

multiset<int> s[N];

#undef int
int main() {
#define int long long int
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  freopen("ip.txt", "r", stdin);
  freopen("op.txt", "w", stdout);
#endif

  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    s[x].insert(i);
  }
  int ans = 0;
  if (s[1].size() == 0) {
    cout << -1 << endl;
    return 0;
  }

  int ind = *s[1].begin();
  int x = 2;
  ans = 1;

  while (1) {
    auto it = s[x].upper_bound(ind);
    if (it == s[x].end())
      break;
    ans++;
    x++;
    ind = *it;
  }

  cout << n - ans << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define int long long int
mt19937 rng(std::chrono::duration_cast<std::chrono::nanoseconds>(
                chrono::high_resolution_clock::now().time_since_epoch())
                .count());
#define mp make_pair
#define pb push_back
#define F first
#define S second
const int N = 200005;
#define M 1000000007
#define double long double
#define BINF 10000000000000000
#define init(arr, val) memset(arr, val, sizeof(arr))
#define MAXN 15000001
#define deb(x) cout << #x << " " << x << "\n";
const int LG = 22;

multiset<int> s[N];

#undef int
int main() {
#define int long long int
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    s[x].insert(i);
  }
  int ans = 0;
  if (s[1].size() == 0) {
    cout << -1 << endl;
    return 0;
  }

  int ind = *s[1].begin();
  int x = 2;
  ans = 1;

  while (1) {
    auto it = s[x].upper_bound(ind);
    if (it == s[x].end())
      break;
    ans++;
    x++;
    ind = *it;
  }

  cout << n - ans << endl;

  return 0;
}
Problem p02832 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using P = pair<int, int>;
using ll = long long;

int main() {
  int n;
  cin >> n;
  vector<int> a;
  rep(i, n) cin >> a[i];
  int nxt = 1, ans = 0;
  rep(i, n) {
    if (a[i] == nxt) {
      nxt++;
    } else
      ans++;
  }

  if (ans == n)
    ans = -1;
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
using P = pair<int, int>;
using ll = long long;

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  rep(i, n) cin >> a[i];
  int nxt = 1, ans = 0;
  rep(i, n) {
    if (a[i] == nxt) {
      nxt++;
    } else
      ans++;
  }

  if (ans == n)
    ans = -1;
  cout << ans << endl;
}
Problem p02832 (C++)

Buggy submission (status: Runtime Error):

#include <bits/stdc++.h>

#define ln '\n'
#define all(dat) dat.begin(), dat.end()
#define loop(i, to) for (int i = 0; i < to; ++i)
#define cont(i, to) for (int i = 1; i <= to; ++i)
#define circ(i, fm, to) for (int i = fm; i <= to; ++i)
#define foreach(i, dat)                                                        \
  for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long num;

using namespace std;

const int nsz = 1e5;
int n, a[nsz + 5];

int main() {
  ios::sync_with_stdio(0);
  cin >> n;
  cont(i, n) { cin >> a[i]; }
  int cur = 0;
  cont(i, n) {
    if (a[i] == cur + 1) {
      ++cur;
    }
  }
  if (cur == 0) {
    cout << -1 << ln;
  } else {
    cout << n - cur << ln;
  }
}

Fixed submission:

#include <bits/stdc++.h>

#define ln '\n'
#define all(dat) dat.begin(), dat.end()
#define loop(i, to) for (int i = 0; i < to; ++i)
#define cont(i, to) for (int i = 1; i <= to; ++i)
#define circ(i, fm, to) for (int i = fm; i <= to; ++i)
#define foreach(i, dat)                                                        \
  for (__typeof(dat.begin()) i = dat.begin(); i != dat.end(); ++i)

typedef long long num;

using namespace std;

const int nsz = 2e5;
int n, a[nsz + 5];

int main() {
  ios::sync_with_stdio(0);
  cin >> n;
  cont(i, n) { cin >> a[i]; }
  int cur = 0;
  cont(i, n) {
    if (a[i] == cur + 1) {
      ++cur;
    }
  }
  if (cur == 0) {
    cout << -1 << ln;
  } else {
    cout << n - cur << ln;
  }
}
Problem p02832 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int x = 1, res, m, n, a[100000], i, b[1000] = {0}, cnt = 0;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] == x) {
      cnt++;
      x++;
    }
    // b[a[i]]++;
  }
  if (cnt != 0)
    cout << n - cnt << endl;
  else
    cout << -1 << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int x = 1, res, m, n, a[200008], i, b[1000] = {0}, cnt = 0;
  cin >> n;
  for (i = 1; i <= n; i++) {
    cin >> a[i];
    if (a[i] == x) {
      cnt++;
      x++;
    }
    // b[a[i]]++;
  }
  if (cnt != 0)
    cout << n - cnt << endl;
  else
    cout << -1 << endl;
}
Problem p02832 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>

int main(int argc, const char *argv[]) {
  int n;
  scanf("%d", &n);

  int a[20000];
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }

  int ans = 0, cur = 1;
  for (int i = 0; i < n; i++) {
    if (a[i] != cur) {
      ans++;
    } else {
      cur++;
    }
  }

  printf("%d\n", ans == n ? -1 : ans);
}

Fixed submission:
#include <cstdio>

int main(int argc, const char *argv[]) {
  int n;
  scanf("%d", &n);

  int a[300000];
  for (int i = 0; i < n; i++) {
    scanf("%d", &a[i]);
  }

  int ans = 0, cur = 1;
  for (int i = 0; i < n; i++) {
    if (a[i] != cur) {
      ans++;
    } else {
      cur++;
    }
  }

  printf("%d\n", ans == n ? -1 : ans);
}
Problem p02832 (C++)

Buggy submission (status: Runtime Error):
// daddy's back in town, back to rolling the damn hash yoooo babeeeeees//
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define dbg(x) cout << #x << '=' << x << '\n';
#define ll long long
#define x first
#define y second
#define pi pair<int, int>
#define vi vector<int>
#define L nod << 1
#define R ((nod << 1) | 1)
const ll mod = 998244353;
#define int ll
int n, m, ans, cnt[100010];

int32_t main() {
  ios_base ::sync_with_stdio(0);
  cin.tie();
  cout.tie();
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  cin >> n;
  cnt[0] = 1;
  for (int i = 1, x; i <= n; i++) {
    cin >> x;
    if (cnt[x - 1])
      cnt[x] = 1;
  }
  for (int i = n; i >= 1; i--)
    if (cnt[i])
      return cout << n - i, 0;
  cout << -1 << endl;
}

Fixed submission:
// daddy's back in town, back to rolling the damn hash yoooo babeeeeees//
#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define dbg(x) cout << #x << '=' << x << '\n';
#define ll long long
#define x first
#define y second
#define pi pair<int, int>
#define vi vector<int>
#define L nod << 1
#define R ((nod << 1) | 1)
const ll mod = 998244353;
#define int ll
int n, m, ans, cnt[200010];

int32_t main() {
  ios_base ::sync_with_stdio(0);
  cin.tie();
  cout.tie();
  mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  cin >> n;
  cnt[0] = 1;
  for (int i = 1, x; i <= n; i++) {
    cin >> x;
    if (cnt[x - 1])
      cnt[x] = 1;
  }
  for (int i = n; i >= 1; i--)
    if (cnt[i])
      return cout << n - i, 0;
  cout << -1 << endl;
}
Problem p02832 (C++)

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

#define ll long long
#define PB push_back
#define PII pair<int, int>
#define MP make_pair
#define all(x) x.begin(), x.end()
#define REP(x, y, z) for (int x = y; x <= z; x++)
#define REPP(x, y, z) for (int x = y; x >= z; x--)
#define F first
#define S second
#define MSET(x, y) memset(x, y, sizeof(x))
#define EB emplace_back
#define maxn 2005

// structure

// declaration

// functions

int main(void) {
#ifdef DBG
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif

  int n;
  int arr[maxn];

  // MSET(dp, 0);

  scanf("%d", &n);

  REP(i, 1, n) {
    scanf("%d", &arr[i]);
    // ans[i] = i;
  }

  int ans = 0;
  int k = 1;

  REP(i, 1, n) {
    if (arr[i] == k) {
      k++;
    } else {
      ans++;
    }
  }

  if (ans == n)
    printf("-1\n");
  else
    printf("%d\n", ans);

  // REP(i, 1, n)
  // {
  //     REP(j, 1, n)
  //     {
  //         if(arr[i] == ans[j])
  //             dp[i][j] = dp[i-1][j-1]+1;
  //         else
  //         {
  //             dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
  //         }

  //     }
  // }

  // printf("%d\n", n-dp[n][n]);

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define PB push_back
#define PII pair<int, int>
#define MP make_pair
#define all(x) x.begin(), x.end()
#define REP(x, y, z) for (int x = y; x <= z; x++)
#define REPP(x, y, z) for (int x = y; x >= z; x--)
#define F first
#define S second
#define MSET(x, y) memset(x, y, sizeof(x))
#define EB emplace_back
#define maxn 200005

// structure

// declaration

// functions

int main(void) {
#ifdef DBG
  freopen("in.in", "r", stdin);
  freopen("out.out", "w", stdout);
#endif

  int n;
  int arr[maxn];

  // MSET(dp, 0);

  scanf("%d", &n);

  REP(i, 1, n) {
    scanf("%d", &arr[i]);
    // ans[i] = i;
  }

  int ans = 0;
  int k = 1;

  REP(i, 1, n) {
    if (arr[i] == k) {
      k++;
    } else {
      ans++;
    }
  }

  if (ans == n)
    printf("-1\n");
  else
    printf("%d\n", ans);

  // REP(i, 1, n)
  // {
  //     REP(j, 1, n)
  //     {
  //         if(arr[i] == ans[j])
  //             dp[i][j] = dp[i-1][j-1]+1;
  //         else
  //         {
  //             dp[i][j] = max(dp[i-1][j], dp[i][j-1]);
  //         }

  //     }
  // }

  // printf("%d\n", n-dp[n][n]);

  return 0;
}
Problem p02832 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
ll num[20005];
int main() {
  int n;
  scanf("%d", &n);
  ll ans = 0;
  ll cnt = 1;
  for (int i = 0; i < n; i++) {
    scanf("%lld", &num[i]);
    if (num[i] == cnt) {
      cnt++;
    } else {
      ans++;
    }
  }

  if (cnt == 1)
    printf("-1");
  else
    printf("%lld", ans);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
ll num[200005];
int main() {
  int n;
  scanf("%d", &n);
  ll ans = 0;
  ll cnt = 1;
  for (int i = 0; i < n; i++) {
    scanf("%lld", &num[i]);
    if (num[i] == cnt) {
      cnt++;
    } else {
      ans++;
    }
  }

  if (cnt == 1)
    printf("-1");
  else
    printf("%lld", ans);
}
Problem p02832 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
using namespace std;

long long n;
long long a[200010];
int now, ans, tmp1, tmp2;
vector<int> vans;

int saiki(int k) {
  now = 1;
  ans = k;
  // if(n-k > tmp2) return 0;
  FOR(i, k, n) {
    if (a[i] != now && a[i] == 1) {
      tmp1 = now;
      tmp2 = ans;
      saiki(i);
      now = tmp1;
      ans = tmp2;
      ans++;
    } else if (a[i] != now)
      ans++;
    else
      now++;
  }
  vans.push_back(ans);

  return 0;
}

int main() {
  cin >> n;
  REP(i, n) cin >> a[i];

  saiki(0);
  sort(vans.begin(), vans.end());
  if (vans[0] == n)
    vans[0] = -1;
  cout << vans[0] << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define REPR(i, n) for (int i = n - 1; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
using namespace std;

long long n;
long long a[200010];
int now, ans, tmp1, tmp2;
vector<int> vans;

int saiki(int k) {
  now = 1;
  ans = k;
  // if(n-k > tmp2) return 0;
  FOR(i, k, n) {
    if (a[i] != now && a[i] == 1) {
      tmp1 = now;
      tmp2 = ans;
      // saiki(i);
      now = tmp1;
      ans = tmp2;
      ans++;
    } else if (a[i] != now)
      ans++;
    else
      now++;
  }
  vans.push_back(ans);

  return 0;
}

int main() {
  cin >> n;
  REP(i, n) cin >> a[i];

  saiki(0);
  sort(vans.begin(), vans.end());
  if (vans[0] == n)
    vans[0] = -1;
  cout << vans[0] << endl;
  return 0;
}
Problem p02832 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
a = list(map(int, input().split()))
K = 0
ans = 0

for i in range(N):
    k = a.pop(0)
    if k == K + 1:
        K = k
    else:
        ans += 1

if K == 0:
    ans = -1

print(ans)


Fixed submission:
N = int(input())
a = list(map(int, input().split()))
K = 0
ans = 0

for i in range(N):
    k = a[i]
    if k == K + 1:
        K = k
    else:
        ans += 1

if K == 0:
    ans = -1

print(ans)
Problem p02832 (Python)

Buggy submission (status: Time Limit Exceeded):
def t(a):
    b = 0
    n = 0
    while n < len(a):
        if a[n] == n + 1:
            n += 1
        else:
            a.pop(n)
            b += 1
    return b if len(a) != 0 else -1


if __name__ == "__main__":
    n = input()
    a = list(map(int, input().split()))
    print(t(a))


Fixed submission:
def t(a):
    b = [False] * len(a)
    c = 1
    for i, j in enumerate(a):
        if j == c:
            b[i] = True
            c += 1

    return b.count(False) if any(b) else -1


if __name__ == "__main__":
    n = input()
    a = list(map(int, input().split()))
    print(t(a))
Problem p02832 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
a = list(map(int, input().split()))
nm = 1
ans = 0
while a:
    x = a.pop(0)
    if x == nm:
        nm += 1
    else:
        ans += 1

if ans == N:
    print(-1)
else:
    print(ans)


Fixed submission:
N = int(input())
a = list(map(int, input().split()))
nm = 1
ans = 0
for i in range(N):
    x = a[i]
    if x == nm:
        nm += 1
    else:
        ans += 1

if ans == N:
    print(-1)
else:
    print(ans)
Problem p02832 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
a = list(map(int, input().split()))
left_max_num = 0
for i in range(1, N + 1):
    if i in a:
        left_max_num = i
        a = a[a.index(i) + 1 :]
    else:
        break
if left_max_num != 0:
    print(N - left_max_num)
else:
    print(-1)


Fixed submission:
N = int(input())
a = list(map(int, input().split()))

num = 0
for item in a:
    if item == (num + 1):
        num += 1

if num != 0:
    print(N - num)
else:
    print(-1)
Problem p02832 (Python)

Buggy submission (status: Time Limit Exceeded):
N = int(input())
a = list(map(int, input().split()))
ans = 0
i = 1
j = 0
while j < N:
    if i not in a[j:]:
        ans += N - j
        break
    nj = a[j:].index(i)
    ans += nj - j
    i += 1
    j = nj + 1
if i == 1:
    print(-1)
else:
    print(ans)


Fixed submission:
N = int(input())
a = list(map(int, input().split()))
ans = 0
x = 1
for i in a:
    if i == x:
        x += 1
    else:
        ans += 1
if x == 1:
    print(-1)
else:
    print(ans)
Problem p02832 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all_map(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define ALL(a) (a).begin(), (a).end()

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  ll pos = 1;
  rep(i, n) {
    while (i + 1 != a[i]) {
      a.erase(a.begin() + i);
      if (a.empty()) {
        cout << -1 << endl;
        return 0;
      }
    }
    if (i + 1 == a.size())
      break;
  }
  cout << n - a.size() << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define all_map(itr, mp) for (auto itr = mp.begin(); itr != mp.end(); itr++)
#define ALL(a) (a).begin(), (a).end()

int main() {
  ll n;
  cin >> n;
  vector<ll> a(n);
  rep(i, n) cin >> a[i];

  ll pos = 0;
  rep(i, n) if (a[i] == pos + 1) pos++;

  if (pos == 0)
    cout << -1 << endl;
  else
    cout << n - pos << endl;
}
Problem p02832 (C++)

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

using namespace std;
typedef long long ll;

#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d %d", &a, &b)
#define sll(a) scanf("%lld", &a)
#define sll2(a, b) scanf("%lld %lld", &a, &b)
const int maxn = 1e5 + 5;
int a[maxn], b[maxn];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    sint(a[i]);
  int sum = 0;
  int k = 1;
  for (int i = 0; i < n; i++) {
    if (a[i] == k)
      k++;
    else
      sum++;
  }
  if (sum != n)
    cout << sum << endl;
  else
    cout << -1 << endl;
  return 0;
}


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

using namespace std;
typedef long long ll;

#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d %d", &a, &b)
#define sll(a) scanf("%lld", &a)
#define sll2(a, b) scanf("%lld %lld", &a, &b)
const int maxn = 1e6 + 5;
int a[maxn], b[maxn];

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++)
    sint(a[i]);
  int sum = 0;
  int k = 1;
  for (int i = 0; i < n; i++) {
    if (a[i] == k)
      k++;
    else
      sum++;
  }
  if (sum != n)
    cout << sum << endl;
  else
    cout << -1 << endl;
  return 0;
}
Problem p02832 (C++)

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

using namespace std;
typedef long long ll;
#define MOD 1000000007
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

ll helper(int cur, int tgt, int n, vector<vector<int>> &pos) {
  if (cur > n)
    return 0;
  ll res = n - cur;

  auto it = lower_bound(pos[tgt].begin(), pos[tgt].end(), cur);
  while (it != pos[tgt].end()) {
    res = min(res, *it - cur + helper(*it + 1, tgt + 1, n, pos));
    it++;
  }

  return res;
}

int main() {
  ll n;
  cin >> n;

  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  // check whether input array contains 1
  bool flg = true;
  for (int i = 0; i < n; i++)
    if (a[i] == 1)
      flg = false;
  if (flg) {
    cout << -1 << endl;
    return 0;
  }

  vector<vector<int>> pos(n + 1);
  for (int i = 0; i < n; i++)
    pos[a[i]].push_back(i);

  ll ans = helper(0, 1, n, pos);
  cout << ans << endl;

  return 0;
}

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

using namespace std;
typedef long long ll;
#define MOD 1000000007
template <typename T1, typename T2> inline void chmin(T1 &a, T2 b) {
  if (a > b)
    a = b;
}
template <typename T1, typename T2> inline void chmax(T1 &a, T2 b) {
  if (a < b)
    a = b;
}

ll helper(int cur, int tgt, int n, vector<vector<int>> &pos) {
  if (cur >= n)
    return 0;
  if (tgt > n)
    return 0;
  ll res = n - cur;

  auto it = lower_bound(pos[tgt].begin(), pos[tgt].end(), cur);
  while (it != pos[tgt].end()) {
    res = min(res, *it - cur + helper(*it + 1, tgt + 1, n, pos));
    it++;
  }

  return res;
}

int main() {
  ll n;
  cin >> n;

  vector<int> a(n);
  for (int i = 0; i < n; i++)
    cin >> a[i];

  // check whether input array contains 1
  bool flg = true;
  for (int i = 0; i < n; i++)
    if (a[i] == 1)
      flg = false;
  if (flg) {
    cout << -1 << endl;
    return 0;
  }

  vector<vector<int>> pos(n + 1);
  for (int i = 0; i < n; i++)
    pos[a[i]].push_back(i);

  ll ans = helper(0, 1, n, pos);
  cout << ans << endl;

  return 0;
}
Problem p02832 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using ii = pair<ll, ll>;

ll a, b, c, i, j, l, m, n, ans, tmp, hoge, fuga, piyo;
unsigned long long k;
ll A[100000];

int main() {
  cin >> n;
  REP(i, n) cin >> A[i];
  k = 1;
  m = 0;
  for (i = 0; i < n; i++) {
    if (A[i] == k) {
      k++;
    } else {
      m++;
    }
  }
  if (k > 1 && m < n) {
    printf("%llu", m);
  } else {
    printf("-1");
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;
#define REP(i, n) for (int i = 0; i < (n); i++)
using ll = long long;
using ii = pair<ll, ll>;

ll a, b, c, i, j, l, m, n, ans, tmp, hoge, fuga, piyo;
unsigned long long k;
ll A[200000];

int main() {
  cin >> n;
  REP(i, n) cin >> A[i];
  k = 1;
  m = 0;
  for (i = 0; i < n; i++) {
    if (A[i] == k) {
      k++;
    } else {
      m++;
    }
  }
  if (k > 1 && m < n) {
    printf("%llu", m);
  } else {
    printf("-1");
  }
  return 0;
}
Problem p02832 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int f[1001];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> f[i];
  }
  int ans = 0, sum = 1;
  for (int i = 1; i <= n; i++) {
    if (f[i] == sum) {
      sum++;
      ans++;
    }
  }
  if (sum == 1) {
    cout << -1;
  } else
    cout << n - ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int f[1000001];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> f[i];
  }
  int ans = 0, sum = 1;
  for (int i = 1; i <= n; i++) {
    if (f[i] == sum) {
      sum++;
      ans++;
    }
  }
  if (sum == 1) {
    cout << -1;
  } else
    cout << n - ans;
  return 0;
}