Problem p02574 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define MAXN 100001

// stores smallest prime factor for every number
int spf[MAXN];

// Calculating SPF (Smallest Prime Factor) for every
// number till MAXN.
// Time Complexity : O(nloglogn)
void sieve() {
  spf[1] = 1;
  for (int i = 2; i < MAXN; i++)

    // marking smallest prime factor for every
    // number to be itself.
    spf[i] = i;

  // separately marking spf for every even
  // number as 2
  for (int i = 4; i < MAXN; i += 2)
    spf[i] = 2;

  for (int i = 3; i * i < MAXN; i++) {
    // checking if i is prime
    if (spf[i] == i) {
      // marking SPF for all numbers divisible by i
      for (int j = i * i; j < MAXN; j += i)

        // marking spf[j] if it is not
        // previously marked
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}

// A O(log n) function returning primefactorization
// by dividing by smallest prime factor at every step
vector<int> getFactorization(int x) {
  vector<int> ret;
  while (x != 1) {
    ret.push_back(spf[x]);
    x = x / spf[x];
  }
  return ret;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  int arr[n];
  sieve();
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  set<int> s;
  ll fg = 0;
  for (int i = 0; i < n; i++) {
    set<int> st;
    vector<int> p = getFactorization(arr[i]);
    for (int j = 0; j < p.size(); j++) {
      if (st.find(p[j]) == st.end() && s.find(p[j]) != s.end()) {
        fg = 1;
        break;
      } else {
        st.insert(p[j]);
        s.insert(p[j]);
      }
    }
    if (fg == 1)
      break;
  }
  if (fg == 0) {
    cout << "pairwise coprime";
  } else {
    int gcd = arr[0];
    for (int i = 1; i < n; i++) {
      gcd = __gcd(arr[i], gcd);
    }
    if (gcd == 1) {
      cout << "setwise coprime";
    } else {
      cout << "not coprime";
    }
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define MAXN 10000001

// stores smallest prime factor for every number
int spf[MAXN];

// Calculating SPF (Smallest Prime Factor) for every
// number till MAXN.
// Time Complexity : O(nloglogn)
void sieve() {
  spf[1] = 1;
  for (int i = 2; i < MAXN; i++)

    // marking smallest prime factor for every
    // number to be itself.
    spf[i] = i;

  // separately marking spf for every even
  // number as 2
  for (int i = 4; i < MAXN; i += 2)
    spf[i] = 2;

  for (int i = 3; i * i < MAXN; i++) {
    // checking if i is prime
    if (spf[i] == i) {
      // marking SPF for all numbers divisible by i
      for (int j = i * i; j < MAXN; j += i)

        // marking spf[j] if it is not
        // previously marked
        if (spf[j] == j)
          spf[j] = i;
    }
  }
}

// A O(log n) function returning primefactorization
// by dividing by smallest prime factor at every step
vector<int> getFactorization(int x) {
  vector<int> ret;
  while (x != 1) {
    ret.push_back(spf[x]);
    x = x / spf[x];
  }
  return ret;
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  int arr[n];
  sieve();
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  set<int> s;
  ll fg = 0;
  for (int i = 0; i < n; i++) {
    set<int> st;
    vector<int> p = getFactorization(arr[i]);
    for (int j = 0; j < p.size(); j++) {
      if (st.find(p[j]) == st.end() && s.find(p[j]) != s.end()) {
        fg = 1;
        break;
      } else {
        st.insert(p[j]);
        s.insert(p[j]);
      }
    }
    if (fg == 1)
      break;
  }
  if (fg == 0) {
    cout << "pairwise coprime";
  } else {
    int gcd = arr[0];
    for (int i = 1; i < n; i++) {
      gcd = __gcd(arr[i], gcd);
    }
    if (gcd == 1) {
      cout << "setwise coprime";
    } else {
      cout << "not coprime";
    }
  }
  return 0;
}