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

#include <iostream>
#define k 10000

using namespace std;

void CountingSort(int a[], int b[], int n) {
  int cnt[k + 1];
  for (int i = 0; i <= k; i++)
    cnt[i] = 0;
  for (int i = 1; i <= n; i++)
    cnt[a[i]]++;
  for (int i = 1; i <= k; i++)
    cnt[i] = cnt[i] + cnt[i - 1];
  for (int j = 1; j <= n; j++) {
    b[cnt[a[j]]] = a[j];
    cnt[a[j]]--;
  }
}

int main() {
  int n;
  cin >> n;
  int a[n + 1], b[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  CountingSort(a, b, n);
  for (int i = 1; i <= n; i++) {
    if (i == 1)
      cout << b[1];
    else
      cout << " " << b[i];
  }
  cout << endl;
}

Error: 0

Fixed submission:

#include <iostream>
#define k 2000000

using namespace std;

void CountingSort(int a[], int b[], int n) {
  int cnt[k + 1];
  for (int i = 0; i <= k; i++)
    cnt[i] = 0;
  for (int i = 1; i <= n; i++)
    cnt[a[i]]++;
  for (int i = 1; i <= k; i++)
    cnt[i] = cnt[i] + cnt[i - 1];
  for (int j = n; j >= 1; j--) {
    b[cnt[a[j]]] = a[j];
    cnt[a[j]]--;
  }
}

int main() {
  int n;
  cin >> n;
  int a[n + 1], b[n + 1];
  for (int i = 1; i <= n; i++)
    cin >> a[i];
  CountingSort(a, b, n);
  for (int i = 1; i <= n; i++) {
    if (i == 1)
      cout << b[1];
    else
      cout << " " << b[i];
  }
  cout << endl;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

/*
 * CountingSort
 */

#include <iostream>
#include <vector>
using namespace std;

void input(vector<int> &A) {
  int n = 0;
  cin >> n;
  A.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
}

void output(vector<int> &out) {
  int size = out.size();
  for (int i = 0; i < size; i++) {
    cout << out[i];
    if (i < size - 1) {
      cout << " ";
    }
  }
  cout << endl;
}

void CountingSort(vector<int> &A, vector<int> &B, int k) {
  int n = A.size();
  // 0?????????
  vector<int> C(k, 0);
  //	for(int i=0; i<k; i++){
  //		C[i]=0;
  //	}
  // C[i] ??? i ???????????°????¨??????????
  for (int j = 0; j < n; j++) {
    C[A[j]]++;
  }
  // C[i] ??? i ??\\????????°???????????°????¨??????????
  for (int i = 1; i < k; i++) {
    C[i] += C[i - 1];
  }
  for (int j = (n - 1); j >= 0; j--) {
    B[C[A[j]] - 1] = A[j];
    C[A[j]]--;
  }
  return;
}

int main() {

  vector<int> vecA;
  input(vecA);
  vector<int> vecB(vecA.size(), 0);
  CountingSort(vecA, vecB, 10000);
  output(vecB);
  return 0;
}

Error: 0

Fixed submission:

/*
 * CountingSort
 */

#include <iostream>
#include <vector>
using namespace std;

void input(vector<int> &A) {
  int n = 0;
  cin >> n;
  A.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
}

void output(vector<int> &out) {
  int size = out.size();
  // size = n;
  for (int i = 1; i <= size; i++) {
    cout << out[i];
    if (i < size) {
      cout << " ";
    }
  }
  cout << endl;
}

void CountingSort(vector<int> &A, vector<int> &B, int k) {
  int n = A.size();
  // 0?????????
  vector<int> C(k, 0);
  //	for(int i=0; i<k; i++){
  //		C[i]=0;
  //	}
  // C[i] ??? i ???????????°????¨??????????
  for (int j = 0; j < n; j++) {
    C[A[j]]++;
  }
  // C[i] ??? i ??\\????????°???????????°????¨??????????
  for (int i = 1; i < k; i++) {
    C[i] += C[i - 1];
  }
  for (int j = (n - 1); j >= 0; j--) {
    B[C[A[j]]] = A[j];
    C[A[j]]--;
  }
  return;
}

int main() {

  vector<int> vecA;
  input(vecA);
  vector<int> vecB(vecA.size(), 0);
  CountingSort(vecA, vecB, 100);
  output(vecB);
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

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

int main(void) {
  int n, i, j, C[MAX + 1];
  scanf("%d", &n);
  unsigned short A[n + 1], B[n + 1];

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

  for (i = 0; i < n; i++) {
    scanf("%hu", &A[i + 1]);
    C[A[i + 1]]++;
  }

  for (i = 1; i <= MAX; 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)
      printf(" ");
    printf("%d", B[i]);
  }
  printf("\\n");

  return 0;
}

Error: 0

Fixed submission:

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

int main(void) {
  int n, i, j, C[MAX + 1];
  scanf("%d", &n);
  int A[n + 1], B[n + 1];

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

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

  for (i = 1; i <= MAX; 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)
      printf(" ");
    printf("%d", B[i]);
  }
  printf("\\n");

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

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define MAX 2000001
#define VMAX 10000

int main() {
  unsigned short *A, *B;

  int C[VMAX + 1];
  int n, i, j;
  scanf("%d", &n);

  A = (unsigned short *)malloc(sizeof(short) * n + 1);
  B = (unsigned short *)malloc(sizeof(short) * n + 1);

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

  for (int i = 0; i < n; i++) {
    scanf("%hu", &A[i + 1]);
    C[A[i + 1]]++;
  }

  for (int i = 1; i <= VMAX; 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 (i = 1; i <= n; i++) {
    if (i > 1)
      printf(" ");
    printf("%d", B[i]);
  }
  printf("\\n");
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
#define MAX 2000001
#define VMAX 10000

int main() {
  unsigned short *A, *B;

  int C[VMAX + 1];
  int n, i, j;
  scanf("%d", &n);

  A = (unsigned short *)malloc(MAX);
  B = (unsigned short *)malloc(MAX);

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

  for (int i = 0; i < n; i++) {
    scanf("%hu", &A[i + 1]);
    C[A[i + 1]]++;
  }

  for (int i = 1; i <= VMAX; 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 (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 <algorithm>
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

const int MAX_N = 2000000;

int main() {
  int n, A[MAX_N] = {0}, B[MAX_N] = {0}, C[10000] = {0}, max;

  scanf("%d", &n);

  max = 0;

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

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

  for (int i = 1; i <= max; i++) {
    C[i] += C[i - 1];
  }

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

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

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <string>

using namespace std;

const int MAX_N = 200000;

int main() {
  int n, A[MAX_N] = {0}, B[MAX_N] = {0}, C[10000] = {0}, max;

  scanf("%d", &n);

  max = 0;

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

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

  for (int i = 1; i <= max; i++) {
    C[i] += C[i - 1];
  }

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

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

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

#include <stdio.h>
#include <stdlib.h>

#define MAX 2000000
#define VMAX 10000

using namespace std;

int main() {
  unsigned short *A, *B;
  int C[VMAX + 1];
  int n, i, j;
  scanf("%d", &n);

  A = (unsigned short *)malloc(sizeof(unsigned short) * n + 1);
  B = (unsigned short *)malloc(sizeof(unsigned short) * n + 1);

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

  for (i = 0; i < n; i++) {
    scanf("%hu", &A[i + 1]);
    C[A[i + 1]]++;
  }

  for (i = 1; i <= VMAX; i++) {
    C[i] = C[i] + C[i - 1];
  }

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

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

  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>
#include <stdlib.h>

#define MAX 2000000
#define VMAX 10000

using namespace std;

int main() {
  unsigned short *A, *B;
  int C[VMAX + 1];
  int n;
  scanf("%d", &n);

  A = (unsigned short *)malloc(sizeof(unsigned short) * n + 1);
  B = (unsigned short *)malloc(sizeof(unsigned short) * n + 1);

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

  for (int i = 0; i < n; i++) {
    scanf("%hu", &A[i + 1]);
    C[A[i + 1]]++;
  }

  for (int i = 1; i <= VMAX; i++) {
    C[i] = C[i] + C[i - 1];
  }

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

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

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

#include <climits>
#include <iostream>
using namespace std;
void CountingSort(int[], int, int);

int main() {
  int n;
  int A[2000001];
  int k;
  cin >> n;
  int max = INT_MIN;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    if (max < A[i])
      max = A[i];
  }
  k = max;
  CountingSort(A, k, n);
  for (int i = 0; i < n; i++) {
    cout << A[i];
    if (i != n - 1)
      cout << " ";
    else
      cout << endl;
  }
}

void CountingSort(int A[], int k, int n) {
  int C[10001], B[2000001];
  for (int i = 0; i <= k; i++) {
    C[i] = 0;
  }
  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; i++) {
    A[i] = B[i];
  }
}

Error: -11

Fixed submission:

#include <climits>
#include <iostream>
using namespace std;
void CountingSort(int[], int, int);

int main() {
  int n;
  int A[1000001];
  int k;
  cin >> n;
  int max = INT_MIN;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
    if (max < A[i])
      max = A[i];
  }
  k = max;
  CountingSort(A, k, n);
  for (int i = 0; i < n; i++) {
    cout << A[i];
    if (i != n - 1)
      cout << " ";
    else
      cout << endl;
  }
}

void CountingSort(int A[], int k, int n) {
  int C[1000001], B[1000001];
  for (int i = 0; i <= k; i++) {
    C[i] = 0;
  }
  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; i++) {
    A[i] = B[i];
  }
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <stdio.h>

#define MAX 2000001
#define vMAX 10000

int main() {
  int n;
  int C[vMAX];
  for (int i = 0; i < vMAX; i++)
    C[i] = 0;

  scanf("%d", &n);
  int *A = new int[n + 1];
  int *B = new int[n + 1];

  for (int i = 1; i <= n; i++) {
    scanf("%d", &A[i]);
    C[A[i]] += 1;
  }

  for (int i = 1; i < vMAX; i++) {
    C[i] = C[i] + C[i - 1];
  }

  for (int i = n; i >= 1; 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");
  delete[] A;
  delete[] B;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <stdio.h>

#define MAX 2000001
#define vMAX 10000

int main() {
  int n;
  int C[vMAX];
  for (int i = 0; i < vMAX; i++)
    C[i] = 0;

  scanf("%d", &n);
  int *A = new int[n];
  int *B = new int[n];

  for (int i = 1; i <= n; i++) {
    scanf("%d", &A[i]);
    C[A[i]] += 1;
  }

  for (int i = 1; i < vMAX; i++) {
    C[i] = C[i] + C[i - 1];
  }

  for (int i = n; i >= 1; 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");
  delete[] A;
  delete[] B;
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

#define MAX 2000001

void Counting_Sort(vector<int> A, int B[], int k) {
  vector<int> C(k, 0);

  int len = (int)A.size();
  for (int j = 0; j < len; j++) {
    C[A[j]]++;
  }

  for (int i = 1; i <= k; i++) {
    C[i] += C[i - 1];
  }

  for (int j = len - 1; j >= 0; j--) {
    B[C[A[j]]] = A[j];
    C[A[j]]--;
  }
}

int main() {
  int n;

  cin >> n;

  vector<int> A(n);
  int B[MAX];
  int k = 0;

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

  Counting_Sort(A, B, k);

  for (int i = 1; i <= (int)A.size(); i++) {
    if (i == 1) {
      cout << B[i];
    } else {
      cout << " " << B[i];
    }
  }
  cout << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

#define MAX 2000001

void Counting_Sort(vector<int> A, vector<int> &B, int k) {
  vector<int> C(k, 0);

  int len = (int)A.size();
  for (int j = 0; j < len; j++) {
    C[A[j]]++;
  }

  for (int i = 1; i <= k; i++) {
    C[i] += C[i - 1];
  }

  B.resize(len);
  for (int j = len - 1; j >= 0; j--) {
    B[C[A[j]]] = A[j];
    C[A[j]]--;
  }
}

int main() {
  int n;

  cin >> n;

  vector<int> A(n), B;
  int k = 0;

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

  Counting_Sort(A, B, k);

  for (int i = 1; i <= (int)B.size(); i++) {
    if (i == 1) {
      cout << B[i];
    } else {
      cout << " " << B[i];
    }
  }
  cout << endl;

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

#include <cstdio>
using namespace std;

#define MAX 200001
#define VAL_MAX 10000

int main() {
  int n = 0;
  scanf("%d", &n);
  int *A = new int[n + 1];
  int count[VAL_MAX + 1] = {0};
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
    count[A[i]]++;
  }
  // ??¨???
  for (int i = 1; i < VAL_MAX + 1; i++) {
    count[i] += count[i - 1];
  }
  // sort
  int *out = new int[n + 1];
  for (int i = n - 1; 0 <= i; i--) {
    // out[c[a[i]]] ???????´?
    out[count[A[i]]] = A[i];
    count[A[i]]--;
  }

  for (int i = 1; i <= n; i++) {
    if (i - 1)
      printf(" ");
    printf("%d", out[i]);
  }
  printf("\\n");
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>

#define MAX 200001
#define VAL_MAX 10000

int main() {
  int n = 0;
  scanf("%d", &n);
  int A[MAX] = {0};
  int count[VAL_MAX + 1] = {0};
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
    count[A[i]]++;
  }
  // ??¨???
  for (int i = 1; i < VAL_MAX + 1; i++) {
    count[i] += count[i - 1];
  }
  // sort
  int out[MAX] = {0};
  for (int i = n - 1; 0 <= i; i--) {
    // out[c[a[i]]] ???????´?
    out[count[A[i]]] = A[i];
    count[A[i]]--;
  }

  for (int i = 1; i <= n; i++) {
    if (i - 1)
      printf(" ");
    printf("%d", out[i]);
  }
  printf("\\n");
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#define V_MAX 10000 + 1
#define N_MAX 2000000 + 1

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

int main() {
  int n, A[N_MAX];

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

  countingSort(n, A);
  for (int i = 1; i <= n; i++) {
    if (i != n)
      printf("%d ", A[i]);
    else
      printf("%d\\n", A[i]);
  }

  return 0;
}


Error: 0

Fixed submission:

#include <cstdio>
#define N_MAX 10000 + 1

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

int main() {
  int n, A[N_MAX];

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

  countingSort(n, A);
  for (int i = 1; i <= n; i++) {
    if (i != n)
      printf("%d ", A[i]);
    else
      printf("%d\\n", A[i]);
  }

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

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

static const int MAX_NUM = 2000000;
static const int MAX_ELEM = 10000;

#if 0
void CountSort(int elem[], int num)
{
    int i;
    int cnt[MAX_ELEM] = {0};
    int elem2[MAX_NUM+1];
    int target;

    for (i = 0; i < num; i++) {
        ++cnt[elem[i]];
    }
    for (i = 1; i <= MAX_ELEM; i++) {
        cnt[i] += cnt[i - 1];
    }

    for (i = num - 1; i >= 0; i--) {
        target = elem[i];
        --cnt[target];
        elem2[cnt[target]] = target;
    }

    printf("%d", elem2[0]);
    for (i = 1; i < num; i++) {
        printf(" %d", elem2[i]);
    }
    printf("\\n");

    return;
}
#endif

int main(void) {
  int i;
  int num;
  int cnt[MAX_ELEM + 1] = {0};
  int target;
  int *elem1, *elem2;

  scanf("%d", &num);

  elem1 = (int *)malloc(sizeof(int) * num);
  elem2 = (int *)malloc(sizeof(int) * num);

  for (i = 0; i < num; i++) {
    scanf("%d", &elem1[i]);
    ++cnt[elem1[i]];
  }

  for (i = 1; i <= MAX_ELEM; i++) {
    cnt[i] += cnt[i - 1];
  }

  for (i = num - 1; i >= 0; i--) {
    target = elem1[i];
    --cnt[target];
    elem2[cnt[target]] = target;
  }

  printf("%d", elem2[0]);
  for (i = 1; i < num; i++) {
    printf(" %d", elem2[i]);
  }
  printf("\\n");

  free(elem1);
  free(elem2);
  //    CountSort(elem, num, maxelem);

  return 0;
}

Error: 0

Fixed submission:

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

static const int MAX_NUM = 20000;
static const int MAX_ELEM = 10000;
#if 0
void CountSort(int elem[], int num)
{
    int i;
    int cnt[MAX_ELEM] = {0};
    int elem2[MAX_NUM+1];
    int target;

    for (i = 0; i < num; i++) {
        ++cnt[elem[i]];
    }
    for (i = 1; i <= MAX_ELEM; i++) {
        cnt[i] += cnt[i - 1];
    }

    for (i = num - 1; i >= 0; i--) {
        target = elem[i];
        --cnt[target];
        elem2[cnt[target]] = target;
    }

    printf("%d", elem2[0]);
    for (i = 1; i < num; i++) {
        printf(" %d", elem2[i]);
    }
    printf("\\n");

    return;
}
#endif
int main(void) {
  int i;
  int num;
  int elem[MAX_NUM];
  int elem2[MAX_NUM];
  int cnt[MAX_ELEM + 1] = {0};
  int target;

  scanf("%d", &num);
  for (i = 0; i < num; i++) {
    scanf("%d", &elem[i]);
    ++cnt[elem[i]];
  }

  for (i = 1; i <= MAX_ELEM; i++) {
    cnt[i] += cnt[i - 1];
  }

  for (i = num - 1; i >= 0; i--) {
    target = elem[i];
    --cnt[target];
    elem2[cnt[target]] = target;
  }

  printf("%d", elem2[0]);
  for (i = 1; i < num; i++) {
    printf(" %d", elem2[i]);
  }
  printf("\\n");

  //    CountSort(elem, num, maxelem);

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

#include "iostream"
#include <algorithm>

using namespace std;

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

int main() {
  int A[2000000], B[2000000];
  int n;
  int cnt = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < n; i++) {
    B[i] = A[i];
    cnt++;
  }
  CountingSort(A, B, n);
  sort(B, B + cnt);
  cout << B[0];
  for (int i = 1; i < n; i++) {
    cout << " ";
    cout << B[i];
  }
  cout << endl;
  return 0;
}


Error: 0

Fixed submission:

#include "iostream"
#include <algorithm>

using namespace std;

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

int main() {
  int A[200], B[200];
  int n;
  int cnt = 0;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  for (int i = 0; i < n; i++) {
    B[i] = A[i];
    cnt++;
  }
  CountingSort(A, B, n);
  sort(B, B + cnt);
  cout << B[0];
  for (int i = 1; i < n; i++) {
    cout << " ";
    cout << B[i];
  }
  cout << endl;
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#define N 10001
using namespace std;

void Counting_Sort(int *A, int n, int *B, int k) {
  int *C = new int[N];
  for (int i = 0; i <= k; i++)
    C[i] = 0;
  for (int j = 0; j < n; j++)
    C[A[j]] = C[A[j]] + 1;
  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]]] = A[j];
    C[A[j]] = C[A[j]] - 1;
  }
  delete[] C;
}

int main() {
  int n;
  cin >> n;
  int *A = new int[n];
  int *B = new int[n + 1];
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  Counting_Sort(A, n, B, N - 1);
  for (int i = 1; i < n + 1; i++) {
    cout << B[i];
    if (i == n)
      cout << endl;
    else
      cout << " ";
  }
  delete[] A;
  delete[] B;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#define N 10001
using namespace std;

void Counting_Sort(int *A, int n, int *B, int k) {
  int *C = new int[N];
  for (int i = 0; i <= k; i++)
    C[i] = 0;
  for (int j = 0; j < n; j++)
    C[A[j]] = C[A[j]] + 1;
  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]]] = A[j];
    C[A[j]] = C[A[j]] - 1;
  }
  delete[] C;
}

int main() {
  int n;
  cin >> n;
  int *A = new int[n];
  int *B = new int[n];
  for (int i = 0; i < n; i++) {
    cin >> A[i];
  }
  Counting_Sort(A, n, B, N - 1);
  for (int i = 1; i < n + 1; i++) {
    cout << B[i];
    if (i == n)
      cout << endl;
    else
      cout << " ";
  }
  delete[] A;
  delete[] B;
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#define MAX 2000001
#define N 10000

int main() {

  int C[N];
  int A[MAX];
  int B[MAX];
  int i, j, num;

  scanf("%d", &num);
  for (i = 1; i <= num; i++) {
    scanf("%d", &A[i]);
  }

  for (i = 0; i < N; i++) {
    C[i] = 0;
  }

  for (j = 1; j <= num; j++) {
    C[A[j]]++;
  }

  for (i = 1; i <= N; i++) {
    C[i] = C[i] + C[i - 1];
  }

  for (j = num; j > 0; j--) {
    B[C[A[j]]] = A[j];
    C[A[j]]--;
  }
  for (i = 1; i < num; i++) {
    printf("%d ", B[i]);
  }
  printf("%d\\n", B[num]);
}

Error: 0

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
#define MAX 200001
#define VMAX 10000

int main() {

  int C[VMAX];
  int A[MAX];
  int B[MAX];
  int i, j, num;

  scanf("%d", &num);
  for (i = 1; i <= num; i++) {
    scanf("%d", &A[i]);
  }

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

  for (j = 1; j <= num; j++) {
    C[A[j]]++;
  }

  for (i = 1; i <= VMAX; i++) {
    C[i] = C[i] + C[i - 1];
  }

  for (j = num; j > 0; j--) {
    B[C[A[j]]] = A[j];
    C[A[j]]--;
  }
  for (i = 1; i < num; i++) {
    printf("%d ", B[i]);
  }
  printf("%d\\n", B[num]);
}