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

#include <iostream>
#include <vector>

using namespace std;
int n;
void countingSort(short A[], short B[], int k) {
  int C[k + 1];
  for (int i = 0; i < k + 1; i++) {
    C[i] = 0;
  }
  for (int j = 0; j < n; j++) {
    C[A[j]]++;
  }
  for (int i = 1; i < k + 1; i++) {
    C[i] += C[i - 1];
  }
  for (int j = n - 1; j >= 0; j--) {
    //      cout<<"A[j]:"<<A[j]<<endl;
    B[C[A[j]] - 1] = A[j];
    C[A[j]] = C[A[j]] - 1;
  }
}
int main() {
  for (; cin >> n;) {
    short data[n];
    short out[n];
    int k = 0;
    for (int i = 0; i < n; i++) {
      cin >> data[i];
      if (k < data[i])
        k = data[i];
    }
    countingSort(data, out, k);
    for (int i = 0; i < n; i++) {
      cout << out[i];
      if (i != n - 1)
        cout << " ";
      else
        cout << endl;
    }
  }
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;
int n;
void countingSort(int A[], int B[], int k) {
  int C[k + 1];
  for (int i = 0; i < k + 1; i++) {
    C[i] = 0;
  }
  for (int j = 0; j < n; j++) {
    C[A[j]]++;
  }
  for (int i = 1; i < k + 1; i++) {
    C[i] += C[i - 1];
  }
  for (int j = n - 1; j >= 0; j--) {
    //      cout<<"A[j]:"<<A[j]<<endl;
    B[C[A[j]] - 1] = A[j];
    C[A[j]] = C[A[j]] - 1;
  }
}
int main() {
  for (; cin >> n;) {
    int data[n];
    int out[n];
    int k = 0;
    for (int i = 0; i < n; i++) {
      cin >> data[i];
      if (k < data[i])
        k = data[i];
    }
    countingSort(data, out, k);
    for (int i = 0; i < n; i++) {
      cout << out[i];
      if (i != n - 1)
        cout << " ";
      else
        cout << endl;
    }
  }
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

using namespace std;

int main() {
  int C[10001];
  for (int i = 0; i < 10001; i++)
    C[i] = 0;
  int n;
  scanf("%d", &n);
  int *A = new int[n];
  int *B = new int[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &A[i]);
    C[A[i]]++;
  }

  for (int i = 0, j = 0; i < 10001; i++) {
    if (C[i] != 0) {
      for (int k = 0; k < C[i]; k++) {
        B[j++] = i;
      }
    }
  }

  for (int i = 0; i < n - 1; i++)
    printf("%d ", B[i]);
  printf("%d\\n", B[n - 1]);
  delete[] A;
  delete[] B;
  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>

using namespace std;

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

  for (int i = 0, j = 0; i < 10001; i++) {
    if (C[i] != 0) {
      for (int k = 0; k < C[i]; k++) {
        B[j++] = i;
      }
    }
  }

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

#include <iostream>
#include <vector>

using namespace std;

static void inputArray(int A[], int num) {
  for (int i = 0; i < num; i++) {
    cin >> A[i];
  }
}

static void printArray(int A[], int num) {
  for (int i = 0; i < num; i++) {
    cout << ((i != 0) ? " " : "") << A[i];
  }
  cout << endl;
}

static vector<int> C(10000 + 1, 0);
static inline void countingSort(int A[], int B[], int num) {
  for (int i = 0; i < num; i++) {
    C[A[i + 1]]++;
  }
  for (int i = 1; i <= 10000; i++) {
    C[i] = C[i] + C[i - 1];
  }
  for (int i = num; i > 0; i--) {
    //  for (int i = 1; i <= num; i++) {
    B[C[A[i]]] = A[i];
    C[A[i]]--;
  }
}

static vector<int> A(2000000 + 1), B(2000000 + 1);

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

  inputArray(&A[1], n);

  countingSort(&A[0], &B[0], n);

  printArray(&B[1], n);

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

static void inputArray(int A[], int num) {
  for (int i = 0; i < num; i++) {
    cin >> A[i];
  }
}

static void printArray(int A[], int num) {
  for (int i = 0; i < num; i++) {
    cout << ((i != 0) ? " " : "") << A[i];
  }
  cout << endl;
}

static vector<int> C(10000 + 1, 0);
static inline void countingSort(int A[], int B[], int num) {
  for (int i = 0; i < num; i++) {
    C[A[i + 1]]++;
  }
  for (int i = 1; i <= 10000; i++) {
    C[i] = C[i] + C[i - 1];
  }
  for (int i = num; i > 0; i--) {
    //  for (int i = 1; i <= num; i++) {
    B[C[A[i]]] = A[i];
    C[A[i]]--;
  }
}

static vector<int> A(2000000 + 1), B(100000 + 1);

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

  inputArray(&A[1], n);

  countingSort(&A[0], &B[0], n);

  printArray(&B[1], n);

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

#include <iostream>
#include <vector>

using namespace std;

static void inputArray(int A[], int num) {
  for (int i = 0; i < num; i++) {
    cin >> A[i];
  }
}

static void printArray(int A[], int num) {
  for (int i = 0; i < num; i++) {
    cout << ((i != 0) ? " " : "") << A[i];
  }
  cout << endl;
}

static vector<int> C(10000 + 1, 0);
static inline void countingSort(int A[], int B[], int num) {
  for (int i = 0; i < num; i++) {
    C[A[i + 1]]++;
  }
  for (int i = 1; i <= 10000; i++) {
    C[i] = C[i] + C[i - 1];
  }
  for (int i = num; i > 0; i--) {
    //  for (int i = 1; i <= num; i++) {
    B[C[A[i]]] = A[i];
    C[A[i]]--;
  }
}

static vector<int> A(2000000 + 1), B(2000000 + 1);

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

  inputArray(&A[1], n);

  countingSort(&A[0], &B[0], n);

  printArray(&B[1], n);

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

static void inputArray(int A[], int num) {
  for (int i = 0; i < num; i++) {
    cin >> A[i];
  }
}

static void printArray(int A[], int num) {
  for (int i = 0; i < num; i++) {
    cout << ((i != 0) ? " " : "") << A[i];
  }
  cout << endl;
}

static vector<int> C(10000 + 1, 0);
static inline void countingSort(int A[], int B[], int num) {
  for (int i = 0; i < num; i++) {
    C[A[i + 1]]++;
  }
  for (int i = 1; i <= 10000; i++) {
    C[i] = C[i] + C[i - 1];
  }
  for (int i = num; i > 0; i--) {
    //  for (int i = 1; i <= num; i++) {
    B[C[A[i]]] = A[i];
    C[A[i]]--;
  }
}

static vector<int> A(10000 + 1), B(10000 + 1);

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

  inputArray(&A[1], n);

  countingSort(&A[0], &B[0], n);

  printArray(&B[1], n);

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

#include <stdio.h>

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

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

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

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

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

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

  return 0;
}


Error: 0

Fixed submission:

#include <stdio.h>

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

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

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

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

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

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

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

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
const int num = 2000001, num1 = 10000;
int main() {
  int n, a[num], c[num1 + 1], b[num];
  cin >> n;
  for (int i = 0; i <= num1; i++)
    c[i] = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    c[a[i]]++;
  for (int i = 1; i <= num1; i++)
    c[i] += c[i - 1];
  for (int i = 0; i < n; i++)
    b[(c[a[i]]--) - 1] = a[i];
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << b[i];
  }
  cout << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
using namespace std;
const int num = 20001, num1 = 10000;
int main() {
  int n, a[num], c[num1 + 1], b[num];
  cin >> n;
  for (int i = 0; i <= num1; i++)
    c[i] = 0;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++)
    c[a[i]]++;
  for (int i = 1; i <= num1; i++)
    c[i] += c[i - 1];
  for (int i = 0; i < n; i++)
    b[(c[a[i]]--) - 1] = a[i];
  for (int i = 0; i < n; i++) {
    if (i)
      cout << " ";
    cout << b[i];
  }
  cout << endl;
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

#define MAX_LEN 2000000
#define MAX_VAL 10000

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

  int idx = 0;
  for (i = 0; i < MAX_VAL; i++) {
    if (C[i] == 0)
      continue;
    for (int j = 0; j < C[i]; j++) {
      B[idx++] = i;
    }
  }
}

int main() {

  int n, A[MAX_LEN], B[MAX_LEN], C[MAX_VAL];

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

  countingSort(n, A, B, C);

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

  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
using namespace std;

#define MAX_LEN 200001
#define MAX_VAL 10001

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

  int idx = 0;
  for (i = 0; i < MAX_VAL; i++) {
    if (C[i] == 0)
      continue;
    for (int j = 0; j < C[i]; j++) {
      B[idx++] = i;
    }
  }
}

int main() {

  int n, A[MAX_LEN], B[MAX_LEN], C[MAX_VAL];

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

  countingSort(n, A, B, C);

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

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

#include <iostream>

using namespace std;

int A[2000000];
int B[2000000];
int C[10001];

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

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

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

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

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

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

int A[200000];
int B[200000];
int C[10001];

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

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

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

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

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

#include <iostream>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int c[10001] = {}; // for count
  int x;
  for (int i = 0; i < n; i++) {
    cin >> x;
    c[x]++;
  }
  bool start = false;
  for (int i = 0; i < 10001; i++) {
    for (int j = 0; j < c[i]; j++) {
      if (start == false) {
        start = true;
      } else {
        cout << " ";
      }
      cout << i;
    }
  }
  cout << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  int a[n];
  int c[10001] = {}; // for count
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    c[a[i]]++;
  }
  for (int i = 1; i < 10001; i++) {
    c[i] = c[i] + c[i - 1];
  }
  int b[n];
  for (int i = 0; i < n; i++) {
    b[c[a[i]] - 1] = a[i];
    c[a[i]]--;
  }

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

#include <iostream>

void countingSort(int *A, int *B, int k, int n) {
  int i;
  int *C;

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

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

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

int main(void) {
  int i, n, k;
  int *A, *B;

  std::cin >> n;
  A = new int[n];
  B = new int[n];
  for (i = 0; i < n; i++) {
    std::cin >> A[i];
  }
  k = A[0];
  for (i = 1; i < n; i++) {
    if (A[i] > k) {
      k = A[i];
    }
  }
  countingSort(A, B, k, n);
  for (i = 0; i < n - 1; i++) {
    std::cout << B[i] << " ";
  }
  std::cout << B[i] << std::endl;
  delete[] A;
  delete[] B;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>

void countingSort(int *A, int *B, int k, int n) {
  int i;
  int *C;

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

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

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

int main(void) {
  int i, n, k;
  int *A, *B;

  std::cin >> n;
  A = new int[n];
  B = new int[n];
  for (i = 0; i < n; i++) {
    std::cin >> A[i];
  }
  k = A[0];
  for (i = 1; i < n; i++) {
    if (A[i] > k) {
      k = A[i];
    }
  }
  B = new int[k];
  countingSort(A, B, k, n);

  for (i = 0; i < n - 1; i++) {
    std::cout << B[i] << " ";
  }
  std::cout << B[i];
  delete[] A;
  delete[] B;
  return 0;
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <cstring>
#include <iostream>

using namespace std;

int n, a[2000009], b[2000009];

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  int c[10009];
  memset(c, 0, sizeof(c));
  for (int i = 0; i < n; i++)
    c[a[i]]++;
  for (int i = 1; i <= 10000; i++) {
    c[i] += c[i - 1];
  }
  for (int i = n - 1; i >= 0; i--) {
    b[c[a[i]] - 1] = a[i];
    c[a[i]]--;
  }
  for (int i = 0; i < n; i++)
    cout << b[i] << (i < n - 1 ? ' ' : '\\n');
}


Error: 0

Fixed submission:

#include <iostream>
#define MAX 2000000

using namespace std;
int B[MAX];

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

int main() {
  int n, MAX_A = 0;
  int A[MAX];
  cin >> n;
  for (int i = 0; i <= n - 1; i++) {
    cin >> A[i];
  }
  CountingSort(A, n, n);
  for (int i = 1; i <= n; i++) {
    cout << B[i];
    if (i != n)
      cout << " ";
    else
      cout << "\\n";
  }
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

int b[2000005];
signed main() {
  int a;
  scanf("%d", &a);
  for (int c = 0; c < a; c++) {
    scanf("%d", &b[c]);
  }
  sort(b, b + a);
  for (int i = 0; i < a; i++) {
    if (i)
      cout << " ";
    cout << b[i];
  }
  puts("");
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;

int b[200005];
signed main() {
  int a;
  scanf("%d", &a);
  for (int c = 0; c < a; c++) {
    scanf("%d", &b[c]);
  }
  sort(b, b + a);
  for (int i = 0; i < a; i++) {
    if (i)
      cout << " ";
    cout << b[i];
  }
  puts("");
}
Problem p02275 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

#define N 2000001

int n;

void Counting_Sort(int a[], int b[], int k) {
  int i, j, c[N];

  for (i = 0; i <= k; i++)
    c[i] = 0;

  for (j = 1; j <= n; j++)
    c[a[j]]++;

  for (i = 1; i <= k; i++)
    c[i] += c[i - 1];

  for (j = n; j > 0; j--) {
    b[c[a[j]]] = a[j];
    c[a[j]]--;
  }
  for (i = 1; i <= n; i++) {
    printf("%d", b[i]);
    if (i != n)
      printf(" ");
  }
  printf("\\n");
}

int main() {
  int i, max = 0;
  static int a[N], b[N];
  scanf("%d", &n);

  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] > max)
      max = a[i];
  }

  Counting_Sort(a, b, max);

  return 0;
}

Error: 0

Fixed submission:

#include <stdio.h>

#define N 20001

int n;

void Counting_Sort(int a[], int b[], int k) {
  int i, j, c[N];

  for (i = 0; i <= k; i++)
    c[i] = 0;

  for (j = 1; j <= n; j++)
    c[a[j]]++;

  for (i = 1; i <= k; i++)
    c[i] += c[i - 1];

  for (j = n; j > 0; j--) {
    b[c[a[j]]] = a[j];
    c[a[j]]--;
  }
  for (i = 1; i <= n; i++) {
    printf("%d", b[i]);
    if (i != n)
      printf(" ");
  }
  printf("\\n");
}

int main() {
  int i, max = 0;
  static int a[N], b[N];
  scanf("%d", &n);

  for (i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
    if (a[i] > max)
      max = a[i];
  }

  Counting_Sort(a, b, max);

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

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX(x, y) (((x) > (y)) ? (x) : (y))

int main() {

  int n, i, max;
  scanf("%d", &n);

  unsigned short A[n + 1];
  unsigned short B[n + 1];

  max = (1 << 30) * -1;
  for (i = 0; i < n; i++) {
    scanf("%hu", &A[i + 1]);
    max = MAX(max, A[i + 1]);
  }
  int C[max + 1];
  memset(C, 0, sizeof(C));

  for (i = 0; i < n; i++)
    C[A[i + 1]]++;
  for (i = 1; i <= max; i++)
    C[i] += C[i - 1];

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

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

  return 0;
}


Error: -11

Fixed submission:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX(x, y) (((x) > (y)) ? (x) : (y))

int main() {

  int n, i, max;
  scanf("%d", &n);

  int A[n + 1];
  int B[n + 1];

  max = (1 << 30) * -1;
  for (i = 0; i < n; i++) {
    scanf("%hu", &A[i + 1]);
    max = MAX(max, A[i + 1]);
  }
  int C[max + 1];
  memset(C, 0, sizeof(C));

  for (i = 0; i < n; i++)
    C[A[i + 1]]++;
  for (i = 1; i <= max; i++)
    C[i] += C[i - 1];

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

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

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

#include <stdio.h>
#define N 10000
int A[2000001], C[N], B[2000001];
int main() {
  int i, n;

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

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

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

Error: 0

Fixed submission:

#include <stdio.h>
#define N 10001
int main() {
  int i, num, j;
  int A[200001];
  int B[200001];
  int C[N];

  scanf("%d", &num);

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

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

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

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

  for (j = num; j >= 0; j--) { // num-1??§????????????
    B[C[A[j]]] = A[j];
    C[A[j]] = C[A[j]] - 1;
  }

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

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

#include <stdio.h>
#define N 10000
int A[2000001], C[N], B[2000001];
int main() {
  int i, n;

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

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

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

Error: 0

Fixed submission:

#include <stdio.h>
#define N 10000
int A[10000], C[N], B[2000001];
int main() {
  int i, n;

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

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

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

#include <stdio.h>
#define N 10000
int A[2000001], C[N], B[2000001];
int main() {
  int i, n;

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

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

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

Error: 0

Fixed submission:

#include <stdio.h>
#define N 10000
int A[200000], C[N], B[2000001];
int main() {
  int i, n;

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

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

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

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

using namespace std;

void counting_sort(vector<int> &A, int n) {

  int k = *max_element(A.begin(), A.end());
  vector<int> B(n + 1);
  vector<int> C(k + 1);

  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]]] = A[j];
    C[A[j]]--;
  }

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

int main() {

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

  counting_sort(A, n);

  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

void counting_sort(vector<int> &A, int n) {

  int k = *max_element(A.begin(), A.end());
  vector<int> B(n);
  vector<int> C(k + 1);

  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]]] = A[j];
    C[A[j]]--;
  }

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

int main() {

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

  counting_sort(A, n);

  return 0;
}