Problem p02574 (C++)

Buggy submission (status: Runtime Error):
// CPP program to find the number of pairs
// such that gcd equals to 1
#include <bits/stdc++.h>
using namespace std;

#define N 100050

int lpf[N], mobius[N];

// Function to calculate least
// prime factor of each number
void least_prime_factor() {
  for (int i = 2; i < N; i++)

    // If it is a prime number
    if (!lpf[i])

      for (int j = i; j < N; j += i)

        // For all multiples which are not
        // visited yet.
        if (!lpf[j])
          lpf[j] = i;
}

// Function to find the value of Mobius function
// for all the numbers from 1 to n
void Mobius() {
  for (int i = 1; i < N; i++) {

    // If number is one
    if (i == 1)
      mobius[i] = 1;
    else {

      // If number has a squared prime factor
      if (lpf[i / lpf[i]] == lpf[i])
        mobius[i] = 0;

      // Multiply -1 with the previous number
      else
        mobius[i] = -1 * mobius[i / lpf[i]];
    }
  }
}

// Function to find the number of pairs
// such that gcd equals to 1
int gcd_pairs(int a[], int n) {
  // To store maximum number
  int maxi = 0;

  // To store frequency of each number
  int fre[N] = {0};

  // Find frequency and maximum number
  for (int i = 0; i < n; i++) {
    fre[a[i]]++;
    maxi = max(a[i], maxi);
  }

  least_prime_factor();
  Mobius();

  // To store number of pairs with gcd equals to 1
  int ans = 0;

  // Traverse through the all possible elements
  for (int i = 1; i <= maxi; i++) {
    if (!mobius[i])
      continue;

    int temp = 0;
    for (int j = i; j <= maxi; j += i)
      temp += fre[j];

    ans += temp * (temp - 1) / 2 * mobius[i];
  }

  // Return the number of pairs
  return ans;
}
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
// Driver code
int main() {
  int n;
  cin >> n;
  int arr[n];
  cin >> arr[0];
  int tmp1 = arr[0];
  for (int j = 1; j < n; j++) {
    cin >> arr[j];
    tmp1 = gcd(tmp1, arr[j]);
  }
  int tmp;
  // Function call
  tmp = gcd_pairs(arr, n);
  // cout<<tmp1<<"\n";
  if (2 * tmp == n * (n - 1)) {
    cout << "pairwise coprime";
  } else if (tmp1 == 1) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }

  return 0;
}


Fixed submission:
// CPP program to find the number of pairs
// such that gcd equals to 1
#include <bits/stdc++.h>
using namespace std;

#define N 1000050

int lpf[N], mobius[N];

// Function to calculate least
// prime factor of each number
void least_prime_factor() {
  for (int i = 2; i < N; i++)

    // If it is a prime number
    if (!lpf[i])

      for (int j = i; j < N; j += i)

        // For all multiples which are not
        // visited yet.
        if (!lpf[j])
          lpf[j] = i;
}

// Function to find the value of Mobius function
// for all the numbers from 1 to n
void Mobius() {
  for (int i = 1; i < N; i++) {

    // If number is one
    if (i == 1)
      mobius[i] = 1;
    else {

      // If number has a squared prime factor
      if (lpf[i / lpf[i]] == lpf[i])
        mobius[i] = 0;

      // Multiply -1 with the previous number
      else
        mobius[i] = -1 * mobius[i / lpf[i]];
    }
  }
}

// Function to find the number of pairs
// such that gcd equals to 1
int gcd_pairs(int a[], int n) {
  // To store maximum number
  int maxi = 0;

  // To store frequency of each number
  int fre[N] = {0};

  // Find frequency and maximum number
  for (int i = 0; i < n; i++) {
    fre[a[i]]++;
    maxi = max(a[i], maxi);
  }

  least_prime_factor();
  Mobius();

  // To store number of pairs with gcd equals to 1
  int ans = 0;

  // Traverse through the all possible elements
  for (int i = 1; i <= maxi; i++) {
    if (!mobius[i])
      continue;

    int temp = 0;
    for (int j = i; j <= maxi; j += i)
      temp += fre[j];

    ans += temp * (temp - 1) / 2 * mobius[i];
  }

  // Return the number of pairs
  return ans;
}
int gcd(int a, int b) {
  if (b == 0)
    return a;
  return gcd(b, a % b);
}
// Driver code
int main() {
  int n;
  cin >> n;
  int arr[n];
  cin >> arr[0];
  int tmp1 = arr[0];
  for (int j = 1; j < n; j++) {
    cin >> arr[j];
    tmp1 = gcd(tmp1, arr[j]);
  }
  int tmp;
  // Function call
  tmp = gcd_pairs(arr, n);
  // cout<<tmp1<<"\n";
  if (2 * tmp == n * (n - 1)) {
    cout << "pairwise coprime";
  } else if (tmp1 == 1) {
    cout << "setwise coprime";
  } else {
    cout << "not coprime";
  }

  return 0;
}