Problem p03244 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <stdlib.h>

#define MAX_LEN (100000)
#define MAX_NUM (120)

int main(int argc, char *argv[]) {
  int n, a[MAX_LEN];
  int cnt_even[MAX_NUM + 1] = {}, cnt_odd[MAX_NUM + 1] = {};
  int max1_cnt_even, max1_cnt_odd, max2_cnt_even, max2_cnt_odd;
  int max1_num_even, max1_num_odd, max2_num_even, max2_num_odd;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < n; i += 2)
    cnt_even[a[i]]++;
  for (int i = 1; i < n; i += 2)
    cnt_odd[a[i]]++;
  max1_cnt_even = -1;
  max1_num_even = -1;
  max2_cnt_even = -1;
  max2_num_even = -1;
  for (int i = 0; i <= MAX_NUM; i++) {
    // printf("i = %d\n", i);
    if (cnt_even[i] > 0) {
      if (max1_cnt_even <= cnt_even[i]) {
        max2_cnt_even = max1_cnt_even;
        max2_num_even = max1_num_even;
        max1_cnt_even = cnt_even[i];
        max1_num_even = i;
      } else if (max2_cnt_even <= cnt_even[i]) {
        max2_cnt_even = cnt_even[i];
        max2_num_even = i;
      }
    }
    // printf("1cnte = %d, 1nume = %d, 2cnte = %d, 2nume = %d\n",
    //        max1_cnt_even, max1_num_even, max2_cnt_even, max2_num_even);
  }
  // printf("1cnte = %d, 1nume = %d, 2cnte = %d, 2nume = %d\n",
  //        max1_cnt_even, max1_num_even, max2_cnt_even, max2_num_even);
  max1_cnt_odd = 0;
  max1_num_odd = -1;
  max2_cnt_odd = 0;
  max2_num_odd = -1;
  for (int i = 0; i <= MAX_NUM; i++) {
    if (cnt_odd[i] > 0) {
      if (max1_cnt_odd <= cnt_odd[i]) {
        max2_cnt_odd = max1_cnt_odd;
        max2_num_odd = max1_num_odd;
        max1_cnt_odd = cnt_odd[i];
        max1_num_odd = i;
      } else if (max2_cnt_odd <= cnt_odd[i]) {
        max2_cnt_odd = cnt_odd[i];
        max2_num_odd = i;
      }
    }
  }
  // printf("1cnto = %d, 1numo = %d, 2cnto = %d, 2numo = %d\n",
  //        max1_cnt_odd, max1_num_odd, max2_cnt_odd, max2_num_odd);
  if (max1_num_even != max1_num_odd) {
    printf("%d\n", n - max1_cnt_odd - max1_cnt_even);
  } else if (max1_cnt_odd + max2_cnt_even >= max2_cnt_odd + max1_cnt_even) {
    printf("%d\n", n - max1_cnt_odd - max2_cnt_even);
  } else {
    printf("%d\n", n - max2_cnt_odd - max1_cnt_even);
  }

  return 0;
}


Fixed submission:
#include <stdio.h>
#include <stdlib.h>

#define MAX_LEN (100000)
#define MAX_NUM (100000)

int main(int argc, char *argv[]) {
  int n, a[MAX_LEN];
  int cnt_even[MAX_NUM + 1] = {}, cnt_odd[MAX_NUM + 1] = {};
  int max1_cnt_even, max1_cnt_odd, max2_cnt_even, max2_cnt_odd;
  int max1_num_even, max1_num_odd, max2_num_even, max2_num_odd;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d", &a[i]);
  for (int i = 0; i < n; i += 2)
    cnt_even[a[i]]++;
  for (int i = 1; i < n; i += 2)
    cnt_odd[a[i]]++;
  max1_cnt_even = -1;
  max1_num_even = -1;
  max2_cnt_even = -1;
  max2_num_even = -1;
  for (int i = 0; i <= MAX_NUM; i++) {
    // printf("i = %d\n", i);
    if (cnt_even[i] > 0) {
      if (max1_cnt_even <= cnt_even[i]) {
        max2_cnt_even = max1_cnt_even;
        max2_num_even = max1_num_even;
        max1_cnt_even = cnt_even[i];
        max1_num_even = i;
      } else if (max2_cnt_even <= cnt_even[i]) {
        max2_cnt_even = cnt_even[i];
        max2_num_even = i;
      }
    }
    // printf("1cnte = %d, 1nume = %d, 2cnte = %d, 2nume = %d\n",
    //        max1_cnt_even, max1_num_even, max2_cnt_even, max2_num_even);
  }
  // printf("1cnte = %d, 1nume = %d, 2cnte = %d, 2nume = %d\n",
  //        max1_cnt_even, max1_num_even, max2_cnt_even, max2_num_even);
  max1_cnt_odd = 0;
  max1_num_odd = -1;
  max2_cnt_odd = 0;
  max2_num_odd = -1;
  for (int i = 0; i <= MAX_NUM; i++) {
    if (cnt_odd[i] > 0) {
      if (max1_cnt_odd <= cnt_odd[i]) {
        max2_cnt_odd = max1_cnt_odd;
        max2_num_odd = max1_num_odd;
        max1_cnt_odd = cnt_odd[i];
        max1_num_odd = i;
      } else if (max2_cnt_odd <= cnt_odd[i]) {
        max2_cnt_odd = cnt_odd[i];
        max2_num_odd = i;
      }
    }
  }
  // printf("1cnto = %d, 1numo = %d, 2cnto = %d, 2numo = %d\n",
  //        max1_cnt_odd, max1_num_odd, max2_cnt_odd, max2_num_odd);
  if (max1_num_even != max1_num_odd) {
    printf("%d\n", n - max1_cnt_odd - max1_cnt_even);
  } else if (max1_cnt_odd + max2_cnt_even >= max2_cnt_odd + max1_cnt_even) {
    printf("%d\n", n - max1_cnt_odd - max2_cnt_even);
  } else {
    printf("%d\n", n - max2_cnt_odd - max1_cnt_even);
  }

  return 0;
}