alexjercan/bugnet huggingface dataset:
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include "iostream"

using namespace std;

int main() {

  short in[2000000];
  short ans[2000000];
  int co[10001];
  int n;

  cin >> n;

  for (int i = 0; i <= 10000; i++) {
    co[i] = 0;
  }

  for (int i = 0; i < n; i++) {
    cin >> in[i];
    co[in[i]]++;
  }

  co[0]--;
  for (int i = 1; i <= 10000; i++) {
    co[i] += co[i - 1];
  }
  for (int i = 0; i < n; i++) {
    ans[co[in[i]]] = in[i];
    co[in[i]]--;
  }

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

  cout << endl;

  return 0;
}

Error: -11

Fixed submission:

#include "iostream"

using namespace std;

int main() {

  int in[2000000];
  int ans[2000000];
  int co[10001];
  int n;

  cin >> n;

  for (int i = 0; i <= 10000; i++) {
    co[i] = 0;
  }

  for (int i = 0; i < n; i++) {
    cin >> in[i];
    co[in[i]]++;
  }

  co[0]--;
  for (int i = 1; i <= 10000; i++) {
    co[i] += co[i - 1];
  }
  for (int i = 0; i < n; i++) {
    ans[co[in[i]]] = in[i];
    co[in[i]]--;
  }

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

  cout << endl;

  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>

#define MAX_VALUE 10001

void counting_sort(std::vector<int> &a) {
  std::vector<int> c(MAX_VALUE, 0);
  for (int i = 0; i < a.size(); i++) {
    c[a[i]]++;
  }
  for (int i = 1; i < c.size(); i++) {
    c[i] = c[i] + c[i - 1];
  }
  std::vector<int> b(a.size());
  for (int i = 0; i < a.size(); i++) {
    b[c[a[i]] - 1] = a[i];
    c[a[i]]--;
  }
  a.swap(b);
}

int main() {
  int n;
  std::cin >> n;
  std::vector<int> a(n);
  for (int i = 0; i < n; i++) {
    std::cin >> a[i];
  }

  counting_sort(a);

  for (int i = 0; i < n - 1; i++) {
    std::cout << a[i] << " ";
  }
  std::cout << a[n - 1] << std::endl;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

#define MAX 10000

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

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

  int b[n];
  for (int i = n - 1; i >= 0; i--) {
    b[c[a[i]] - 1] = a[i];
    c[a[i]]--;
  }

  for (int i = 0; i < n - 1; i++) {
    cout << b[i] << " ";
  }
  cout << b[n - 1] << endl;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  int k = 10001;
  cin >> n;
  vector<unsigned short> A(n + 1);
  vector<unsigned short> B(n + 1);
  int C[10005];
  for (int i = 0; i < 10005; i++)
    C[i] = 0;
  for (int j = 1; j <= n; j++) {
    cin >> A[j];
    C[A[j]]++;
  }
  // cout << 1 << endl;
  for (int i = 1; i <= k; i++) {
    C[i] = C[i] + C[i - 1];
  }
  for (int j = 1; j <= n; j++) {
    B[C[A[j]]] = A[j];
    C[A[j]]--;
  }

  for (int i = 1; i <= n; i++) {
    if (i - 1)
      cout << " ";
    cout << B[i];
  }
  cout << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int n;
void countingsort(int *A, int *B, int k) {
  int C[10005];
  for (int i = 0; i < 10005; i++)
    C[i] = 0;
  for (int j = 1; j <= n; j++) {
    C[A[j]]++;
  }
  for (int i = 1; i <= k; i++) {
    C[i] = C[i] + C[i - 1];
  }
  for (int j = n; j >= 1; j--) {
    B[C[A[j]]] = A[j];
    C[A[j]]--;
  }
}

int main() {
  int k = 2000000;
  int A[200005];
  int B[200005];
  // cout << "a";
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
  }
  countingsort(A, B, k);
  for (int i = 1; i <= n; i++) {
    if (i - 1)
      cout << " ";
    cout << B[i];
  }
  cout << endl;
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>

int main() {
  int n;
  scanf("%d", &n);
  short a[n + 1];
  int c[10001];
  for (int i = 0; i < 10001; i++) {
    c[i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%hd", &a[i]);
  }

  for (int i = 1; i <= n; i++) {
    c[a[i]]++;
  }

  for (int i = 1; i < 10001; i++) {
    c[i] = c[i] + c[i - 1];
  }

  short b[n + 1];

  for (int i = n; i > 0; i--) {
    b[c[a[i]]] = a[i];
    c[a[i]]--;
  }

  for (int i = 1; i <= n; i++) {
    if (i > 1) {
      printf(" ");
    }
    printf("%d", b[i]);
  }
  printf("\\n");

  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>

int main() {
  int n;
  scanf("%d", &n);
  int a[n + 1], c[10001];
  for (int i = 0; i < 10001; i++) {
    c[i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  for (int i = 1; i <= n; i++) {
    c[a[i]]++;
  }
  for (int i = 1; i < 10001; i++) {
    c[i] = c[i] + c[i - 1];
  }
  int b[n + 1];
  for (int i = n; i > 0; i--) {
    b[c[a[i]]] = a[i];
    c[a[i]]--;
  }
  for (int i = 1; i <= n; i++) {
    if (i > 1) {
      printf(" ");
    }
    printf("%d", b[i]);
  }
  printf("\\n");
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
#define MAX 2000000
int a[MAX], b[MAX], c[10010];
int main() {
  int i, n, sum;

  cin >> n;
  sum = 0;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > sum)
      sum = a[i] + 1;
    c[a[i]]++;
  }
  for (i = 1; i <= sum; i++) {
    c[i] += c[i - 1];
  }
  for (i = 0; i < n; i++) {
    b[--c[a[i]]] = a[i];
  }
  for (i = 0; i < n; i++)
    i == n - 1 ? cout << b[i] << endl : cout << b[i] << " ";
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
#define MAX 10000

int main() {
  int i, n, a[MAX], c[MAX], b[MAX], sum;
  cin >> n;
  sum = 0;
  for (i = 0; i < n; i++) {
    cin >> a[i];
    if (a[i] > sum)
      sum = a[i] + 1;
    c[a[i]]++;
  }
  for (i = 1; i <= sum; i++) {
    c[i] += c[i - 1];
  }
  for (i = 0; i < n; i++) {
    b[--c[a[i]]] = a[i];
  }
  for (i = 0; i < n; i++)
    i == n - 1 ? cout << b[i] << endl : cout << b[i] << " ";
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#define MAX 10000
using namespace std;

int n;

void countingSort(short A[], short B[], int k) {
  int C[k + 1];
  for (int i = 0; i <= k; i++) {
    C[i] = 0;
  }
  for (int j = 1; j <= n; j++) {
    C[A[j]] += 1;
  }
  for (int i = 1; i <= k; i++) {
    C[i] += C[i - 1];
  }

  for (int j = 1; j <= n; j++) {
    B[C[A[j]]] = A[j];
    C[A[j]]--;
  }
}

int main() {
  int k = MAX;

  cin >> n;
  short A[n + 1], B[n + 1];

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

  countingSort(A, B, k);
  for (int i = 1; i <= n; i++) {
    if (i > 1)
      cout << " ";
    cout << B[i];
  }
  cout << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#define MAX 10000
using namespace std;

int n;

void countingSort(int A[], int B[], int k) {
  int C[k + 1];
  for (int i = 0; i <= k; i++) {
    C[i] = 0;
  }
  for (int j = 1; j <= n; j++) {
    C[A[j]] += 1;
  }
  for (int i = 1; i <= k; i++) {
    C[i] += C[i - 1];
  }

  for (int j = 1; j <= n; j++) {
    B[C[A[j]]] = A[j];
    C[A[j]]--;
  }
}

int main() {
  int k = MAX;

  cin >> n;
  int A[n + 1], B[n + 1];
  for (int i = 1; i <= n; i++) {
    cin >> A[i];
  }

  countingSort(A, B, k);
  for (int i = 1; i <= n; i++) {
    if (i > 1)
      cout << " ";
    cout << B[i];
  }
  cout << endl;

  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stdlib.h>

#define MAX 2000000

using namespace std;

void printArray(int Array[], int n) {

  int i;
  for (i = 0; i < n - 1; i++) {
    cout << Array[i] << ' ';
  }
  cout << Array[n - 1] << endl;
}

void CountingSort(int A[], int B[], int n, int k) {

  int i;
  int *C;

  C = (int *)malloc(sizeof(int) * (k + 1));

  for (i = 0; i <= k; i++) {
    C[i] = 0;
  }
  for (i = 0; i < n; i++) {
    C[A[i]]++;
  }
  for (i = 1; i <= k; i++) {
    C[i] += C[i - 1];
  }
  for (i = n - 1; i >= 0; i--) {
    B[C[A[i]] - 1] = A[i];
    C[A[i]]--;
  }

  free(C);
}

int main() {

  int i;
  int n;
  int k = 0;
  int *A;
  int *B;

  A = (int *)malloc(sizeof(int) * MAX);
  B = (int *)malloc(sizeof(int) * MAX);

  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> A[i];
    if (A[i] > k) {
      k = A[i];
    }
  }

  CountingSort(A, B, n, k);

  printArray(B, n);

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <stdlib.h>

#define MAX 1000000

using namespace std;

void printArray(int Array[], int n) {

  int i;
  for (i = 0; i < n - 1; i++) {
    cout << Array[i] << ' ';
  }
  cout << Array[n - 1] << endl;
}

void CountingSort(int A[], int B[], int n, int k) {

  int i;
  int *C;

  C = (int *)malloc(sizeof(int) * (k + 1));

  for (i = 0; i <= k; i++) {
    C[i] = 0;
  }
  for (i = 0; i < n; i++) {
    C[A[i]]++;
  }
  for (i = 1; i <= k; i++) {
    C[i] += C[i - 1];
  }
  for (i = n - 1; i >= 0; i--) {
    B[C[A[i]] - 1] = A[i];
    C[A[i]]--;
  }

  free(C);
}

int main() {

  int i;
  int n;
  int k = 0;
  int A[MAX];
  int B[MAX];

  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> A[i];
    if (A[i] > k) {
      k = A[i];
    }
  }

  CountingSort(A, B, n, k);

  printArray(B, n);

  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

const int MAX = 2000001;
const int VMAX = 10000;
int A[MAX], B[MAX];
int C[VMAX + 1];

int main() {
  ios::sync_with_stdio(false);

  int n, i, j;
  cin >> n;

  for (i = 0; i <= VMAX; i++)
    C[i] = 0;

  for (i = 0; i < n; i++) {
    cin >> A[i + 1];
    C[A[i + 1]]++;
  }

  for (i = 1; i <= VMAX; i++) {
    C[i] = C[i] + C[i - 1];
  }
  for (j = 1; j <= n; j++) {
    B[C[A[j]]] = A[j];
    C[A[j]]--;
  }
  for (i = 1; i <= n; i++) {
    if (i > 1)
      cout << " ";
    cout << B[i];
  }
  cout << "\\n";

  return 0;
}

Error: -11

Fixed submission:

#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  // FILE* fp_in = freopen("./input2.txt", "r", stdin);

  const int MAX = 2000001;
  const int VMAX = 10000;

  int C[VMAX + 1];
  int n, i, j;
  cin >> n;
  int A[MAX], B[MAX];

  for (i = 0; i <= VMAX; i++)
    C[i] = 0;

  for (i = 0; i < n; i++) {
    cin >> A[i + 1];
    C[A[i + 1]]++;
  }

  for (i = 1; i <= VMAX; i++) {
    C[i] = C[i] + C[i - 1];
  }
  for (j = 1; j <= n; j++) {
    B[C[A[j]]] = A[j];
    C[A[j]]--;
  }
  for (i = 1; i <= n; i++) {
    if (i > 1)
      cout << " ";
    cout << B[i];
  }
  cout << " \\n";

  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <array>
#include <iostream>
#include <string>

using namespace std;

int n;
array<short, 2000001> A;
array<short, 2000001> B;
array<int, 10001> C;

void CountingSort(int k) {

  for (int j = 0; j < n; j++) {
    C[A[j]]++;
  }
  for (int i = 1; i <= k; i++) {
    C[i] = C[i] + C[i - 1];
  }
  for (int j = n - 1; j >= 0; j--) {
    B[C[A[j]] - 1] = A[j];
    C[A[j]]--;
  }
  for (int i = 0; i < n - 1; i++)
    cout << B.at(i) << " ";
  cout << B.at(n - 1) << endl;
}

int main() {
  int k = -1;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    if (k < A[i])
      k = A[i];
  }
  CountingSort(k);
  return 0;
}

Error: 0

Fixed submission:

#include <array>
#include <iostream>
#include <string>

using namespace std;

int n;
array<short, 2000001> A;
array<short, 2000001> B;
array<short, 10001> C;

void CountingSort(int k) {

  for (int j = 0; j < n; j++) {
    C[A[j]]++;
  }
  for (int i = 1; i <= k; i++) {
    C[i] = C[i] + C[i - 1];
  }
  for (int j = n - 1; j >= 0; j--) {
    B[C[A[j]] - 1] = A[j];
    C[A[j]]--;
  }
  for (int i = 0; i < n - 1; i++)
    cout << B.at(i) << " ";
  cout << B.at(n - 1) << endl;
}

int main() {
  int k = -1;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    if (k < A[i])
      k = A[i];
  }
  CountingSort(k);
  return 0;
}
Problem p02276 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stdlib.h>

#define MAX 100000

using namespace std;

int partition(int A[], int p, int r) {

  int i;
  int j;
  int x;
  int temp;

  x = A[r];
  i = p - 1;

  for (j = p; j < r; j++) {
    if (A[j] <= x) {
      i = i + 1;
      temp = A[i];
      A[i] = A[j];
      A[j] = temp;
    }
  }

  temp = A[i + 1];
  A[i + 1] = A[r];
  A[r] = temp;
  return i + 1;
}

int main() {

  int i;
  int n;
  int q;
  int *A;

  A = (int *)malloc(sizeof(int) * MAX);

  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> A[i];
  }

  q = partition(A, 0, n - 1);

  for (i = 0; i < n - 1; i++) {
    if (i == q) {
      cout << '[' << A[i] << "] ";
    } else {
      cout << A[i] << ' ';
    }
  }
  cout << A[n - 1] << endl;

  free(A);

  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <stdlib.h>

#define MAX 100000

using namespace std;

void printArray(int Array[], int n) {

  int i;
  for (i = 0; i < n - 1; i++) {
    cout << Array[i] << ' ';
  }
  cout << Array[n - 1] << endl;
}

int partition(int A[], int p, int r) {

  int i;
  int j;
  int x;
  int temp;

  x = A[r];

  i = p - 1;

  for (j = p; j < r; j++) {
    if (A[j] <= x) {
      i = i + 1;
      temp = A[i];
      A[i] = A[j];
      A[j] = temp;
    }
  }
  temp = A[i + 1];
  A[i + 1] = A[r];
  A[r] = temp;
  return i + 1;
}

int main() {

  int i;
  int n;
  int *A;
  int q;

  A = (int *)malloc(sizeof(int));

  cin >> n;

  for (i = 0; i < n; i++) {
    cin >> A[i];
  }

  q = partition(A, 0, n - 1);

  for (i = 0; i < n - 1; i++) {
    if (i == q) {
      cout << '[' << A[i] << "] ";
    } else {
      cout << A[i] << ' ';
    }
  }
  cout << A[n - 1] << endl;

  free(A);

  return 0;
}
Problem p02276 (C++) — buggy submission, verdict: Runtime Error

#include <cstdlib>
#include <iostream>
#include <list>
#include <stack>
#include <string>
using namespace std;
int main() {
  int n, s[100000], small = 0, large = 0, cnt = 0;
  cin >> n;
  bool b = true;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < n; i++) {
    if (s[i] <= s[n - 1]) {
      small = i;
      cnt++;
      if (small > large) {
        swap(s[small], s[large]);
        large += 1;
      }
    } else if (b == true) {
      b = false;
      large = i;
    }
    if (b == true) {
      large++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (i == cnt - 1) {
      printf(" [%d]", s[i]);
    } else {
      if (i != 0) {
        cout << " ";
      }
      cout << s[i];
    }
  }
  cout << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <cstdlib>
#include <iostream>
#include <list>
#include <stack>
#include <string>
using namespace std;
int main() {
  int n, s[10000], small = 0, large = 0, cnt = 0;
  cin >> n;
  bool b = true;
  for (int i = 0; i < n; i++) {
    cin >> s[i];
  }
  for (int i = 0; i < n; i++) {
    if (s[i] <= s[n - 1]) {
      small = i;
      cnt++;
      if (small > large) {
        swap(s[small], s[large]);
        large += 1;
      }
    } else if (b == true) {
      b = false;
      large = i;
    }
    if (b == true) {
      large++;
    }
  }
  for (int i = 0; i < n; i++) {
    if (i == cnt - 1) {
      printf(" [%d]", s[i]);
    } else {
      if (i != 0) {
        cout << " ";
      }
      cout << s[i];
    }
  }
  cout << endl;
  return 0;
}
Problem p02276 (C++) — buggy submission, verdict: Runtime Error


#include <iostream>
#include <vector>

using ull = unsigned long long;
using ui = unsigned int;

int main() {
  ui n;
  std::cin >> n;

  std::vector<ull> v;
  v.resize(n);

  for (auto &e : v)
    std::cin >> e;

  /*
          A => 配列。subscriptable。
          p => パーティションしたい配列の先頭
          r => 基準値
  */
  auto partition = [](auto &A, ui p, ui r) {
    auto &x = A[r - 1];
    ui i = p - 1;
    /*
            基準値より小さいものを前に送って
            その個数（位置）を++する( = i )
    */
    for (ui j{p}; j < r - 1; ++j)
      if (A[j] <= x) {
        ++i;
        std::swap(A[i], A[j]);
      }

    /*
            この時点で[p,i)には基準値以下、
            [i,r]には基準値以上しかない。
            最後に基準値を[i,r]の先頭(=i)に持ってくれば
            仕切りにできる。
    */
    ++i;
    std::swap(A[i], A[r - 1]);

    return i;
  };

  auto x = partition(v, 0, v.size());
  //	std::cout << x << "\\n";
  ui i = 0;
  for (auto &e : v) {
    if (i == x)
      std::cout << "[" << e << "]";
    else
      std::cout << e;

    ++i;
    std::cout << (i < n ? " " : "\\n");
  }

  return 0;
}


Error: 0

Fixed submission:


#include <iostream>
#include <vector>

using ull = unsigned long long;
using ui = unsigned int;

int main() {
  ui n;
  std::cin >> n;

  std::vector<ull> v;
  v.resize(n);

  for (auto &e : v)
    std::cin >> e;

  /*
          A => 配列。subscriptable。
          p => パーティションしたい配列の先頭
          r => 基準値
  */
  auto partition = [](auto &A, ui p, ui r) {
    auto &x = A[r];
    /*
            基準値より小さいものを前に送って
            その個数（位置）を++する( = i )
    */
    ui i = p;
    for (ui j{p + 1}; j < r; ++j)
      if (A[j] <= x) {
        std::swap(A[i], A[j]);
        i++;
      }

    /*
            この時点で[p,i)には基準値以下、
            [i,r]には基準値以上しかない。
            最後に基準値を[i,r]の先頭(=i)に持ってくれば
            仕切りにできる。
    */
    std::swap(A[i], A[r]);

    return i;
  };

  auto x = partition(v, 0, 12 - 1);
  //	std::cout << x << "\\n";
  ui i = 0;
  for (auto &e : v) {
    if (i == x)
      std::cout << "[" << e << "]";
    else
      std::cout << e;

    ++i;
    std::cout << (i < n ? " " : "\\n");
  }

  return 0;
}
Problem p02276 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
int n;
int A[100000];
int partition(int p, int r) {
  int x = A[r];
  int i = p - 1;
  for (int j = p; j < r; j++) {
    if (A[j] <= x) {
      i = i + 1;
      swap(A[i], A[j]);
    }
  }
  swap(A[i + 1], A[r]);
  return i + 1;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  int pos = partition(0, n - 1);
  for (int i = 0; i < pos; i++)
    cout << A[i] << " ";
  cout << "[" << A[pos] << "] ";
  for (int i = pos + 1; i < n - 1; i++)
    cout << A[i] << " ";
  cout << A[n - 1] << endl;
}


Error: -11

Fixed submission:

#include <iostream>
using namespace std;
int n;
int A[100000];
int partition(int p, int r) {
  int x = A[r];
  int i = p - 1;
  for (int j = p; r - 1; j++) {
    if (A[j] <= x) {
      i = i + 1;
      swap(A[i], A[j]);
    }
  }
  swap(A[i + 1], A[r]);
  return i + 1;
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> A[i];
  int pos = partition(0, n);
  for (int i = 0; i < pos; i++)
    cout << A[i] << " ";
  cout << "[" << A[pos] << "] ";
  for (int i = pos + 1; i < n; i++)
    cout << A[i] << " ";
  cout << endl;
}
Problem p02276 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int partition(int A[], int p, int r) {
  int x = A[r];
  int i = p - 1;
  for (int j = p; j < r; j++) {
    if (A[j] <= x) {
      i = i + 1;
      swap(A[i], A[j]);
    }
  }
  swap(A[i + 1], A[r]);
  return i + 1;
}

int main() {
  int num = 0;
  cin >> num;
  int array[num];
  for (int i = 0; i < num; i++) {
    cin >> array[i];
  }

  int index = partition(array, 0, num - 1);

  for (int i = 0; i < num; i++) {
    if (i == index)
      cout << "[" << array[i] << "]";
    else {
      cout << array[i];
    }
    if (i != num - 1) {
      cout << " ";
    }
  }
  cout << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int partition(int A[], int p, int r) {

  int x = A[r];
  int i = p - 1;
  for (int j = p; j < r - 1; i++) {
    if (A[j] <= x) {
      i = i + 1;
      swap(A[i], A[j]);
    }
  }
  swap(A[i + 1], A[r]);

  return i + 1;
}

int main() {
  int num = 0;
  cin >> num;
  int array[num];
  for (int i = 0; i < num; i++) {
    cin >> array[i];
  }
  int index = partition(array, 0, num);
  for (int i = 0; i < num; i++) {
    if (i == index)
      cout << "[" << array[i] << "] ";
    else {
      cout << array[i] << " ";
    }
  }
  return 0;
}
Problem p02276 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#define MAX 100000
int A[MAX], n;
int partition(int p, int r) {
  int x, i, j, t;
  x = A[r];
  i = p - 1;
  for (j = p; j < r; j++) {
    if (A[j] <= x) {
      i++;
      t = A[j];
      A[j] = A[i];
      A[i] = t;
    }
  }
  t = A[i + 1];
  A[i + 1] = A[r];
  A[r] = t;
  return i + 1;
}
int main() {
  int i, q;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &A[i]);
  q = partition(0, n - 1);
  for (i = 0; i < n; i++) {
    if (i)
      printf(" ");
    if (i == q)
      printf("[");
    printf("%d", A[i]);
    if (i == q)
      printf("]");
  }
  printf("\\n");
  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
#define MAX 10000
int A[MAX], n;
int h(int p, int r) {
  int x, i, j, t;
  x = A[r];
  i = p - 1;
  for (j = p; j < r; j++) {
    if (A[j] <= x) {
      i++;
      swap(A[i], A[j]);
    }
  }
  swap(A[i + 1], A[r]);
  return i + 1;
}
int main() {
  int i, q;
  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &A[i]);
  q = h(0, n - 1);
  for (i = 0; i < n; i++) {
    if (i)
      printf(" ");
    if (i == q)
      printf("[");
    printf("%d", A[i]);
    if (i == q)
      printf("]");
  }
  printf("\\n");
  return 0;
}
Problem p02276 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#define MAX 100000

int A[MAX], n;

int partition(int p, int r) {
  int x, i, j, t;
  x = A[r];
  i = p - 1;
  for (j = p; j < r; j++) {
    if (A[j] <= x) {
      i++;
      t = A[i];
      A[i] = A[j];
      A[j] = t;
    }
  }
  t = A[i + 1];
  A[i + 1] = A[r];
  A[r] = t;
  return i + 1;
}

int main() {
  int i, q;

  scanf("%d", &n);
  for (i = 0; i < n; i++)
    scanf("%d", &A[i]);

  q = partition(0, n - 1);

  for (i = 0; i < n; i++) {
    if (i)
      printf(" ");
    if (i == q)
      printf("[");
    printf("%d", A[i]);
    if (i == q)
      printf("]");
  }
  printf("\\n");

  return 0;
}

Error: -11

Fixed submission:

#include <stdio.h>
#define MAX 100000

int A[MAX], n;

int partition(int p, int r) {
  int x, i, j, t;
  x = A[r];
  i = p - 1;
  for (j = p; j < r; j++) {
    if (A[j] <= x) {
      i++;
      t = A[i];
      A[i] = A[j];
      A[j] = t;
    }
  }
  t = A[i + 1];
  A[i + 1] = A[j];
  A[j] = t;
  return i + 1;
}

int main() {
  int i, q;

  scanf("%d", n);
  for (i = 0; i < n; i++)
    scanf("%d", &A[i]);

  q = partition(0, n - 1);

  for (i = 0; i < n; i++) {
    if (i)
      printf(" ");
    if (i == q)
      printf("[");
    printf("%d", A[i]);
    if (i == q)
      printf("]");
  }
  printf("\\n");

  return 0;
}