Problem p02574 (C++) — buggy submission, verdict: Runtime Error

/******

AUTHOR : RUDRANSH TRIPATHI, IIIT NAGPUR, CCID(rt24) & CFID(rsgt24)

MOTTO : Use criticism as fuel and you will never run out of energy.
        AIM : TO BECOME A BETTER CODER
        INTROSPECT + COURSE CORRECTIONS = CATALYST FOR SUCCESS
        Every moment is an opportunity to better yourself

*******/

#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
const int MAX = 1e5 + 5;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define int long long int
#define pb push_back
#define d(x) cout << x << "\\n"

// basic mathematics
int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
int lcm(int a, int b) { return (a * b) / gcd(a, b); }

// modular arithmetic
int powermod(int x, int y, int p) {
  int res = 1;

  x = x % p;

  if (x == 0)
    return 0;

  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

// Prime numbers till n precomputation - range query
int prefix[MAX + 1];

void buildPrefix() {
  bool prime[MAX + 1];
  memset(prime, true, sizeof(prime));

  for (int p = 2; p * p <= MAX; p++) {
    if (prime[p] == true) {
      for (int i = p * 2; i <= MAX; i += p)
        prime[i] = false;
    }
  }
  prefix[0] = prefix[1] = 0;
  for (int p = 2; p <= MAX; p++) {
    prefix[p] = prefix[p - 1];
    if (prime[p])
      prefix[p]++;
  }
}

int query(int L, int R) { return prefix[R] - prefix[L - 1]; }

// Find all paths from a to b in an undirected graph
vector<int> g[MAX];
int visited[MAX];
// count of all the paths from 1 to n;

void dfs(int visited[MAX], int u, int v, vector<int> path) {
  visited[u] = 1;
  if (u == v) {
    for (int i = 0; i < path.size(); i++) {
      cout << path[i] << " ";
    }
    cout << "\\n";
  } else {
    for (int i = 0; i < g[u].size(); i++) {
      int y = g[u][i];

      if (visited[y] == 0) {
        visited[y] = 1;
        path.pb(y);
        dfs(visited, y, v, path);
        path.pop_back();
      }
    }
  }

  visited[u] = 0;
}
#define N 1000000
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 findProductSum(int A[], int n) {
  // calculating array sum (a1 + a2  ... + an)
  int array_sum = 0;
  for (int i = 0; i < n; i++)
    array_sum = (array_sum + A[i]) % M;

  // calcualting square of array sum
  // (a1 + a2 + ... + an)^2
  int array_sum_square = (array_sum * array_sum) % M;

  // calcualting a1^2 + a2^2 + ... + an^2
  int individual_square_sum = 0;
  for (int i = 0; i < n; i++)
    individual_square_sum += (A[i] * A[i]) % M;

  // required sum is (array_sum_square -
  // individual_square_sum) / 2
  return ((array_sum_square - individual_square_sum % M) / 2) % M;
}

int findGCD(int arr[], int n) {
  int result = arr[0];
  for (int i = 1; i < n; i++) {
    result = gcd(arr[i], result);

    if (result == 1) {
      return 1;
    }
  }
  return result;
}

void solve() {
  int n;
  cin >> n;
  int ar[n];

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

  int yy = findGCD(ar, n);
  int xx = gcd_pairs(ar, n);
  if (xx == (n * (n - 1)) / 2)
    cout << "pairwise coprime"
         << "\\n";
  else if (yy == 1)
    cout << "setwise coprime"
         << "\\n";
  else
    cout << "not coprime"
         << "\\n";
}

int32_t main(void) {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastio int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}

Error: -11

Fixed submission:

/******

AUTHOR : RUDRANSH TRIPATHI, IIIT NAGPUR, CCID(rt24) & CFID(rsgt24)

MOTTO : Use criticism as fuel and you will never run out of energy.
        AIM : TO BECOME A BETTER CODER
        INTROSPECT + COURSE CORRECTIONS = CATALYST FOR SUCCESS
        Every moment is an opportunity to better yourself

*******/

#include <bits/stdc++.h>
using namespace std;
const int M = 1e9 + 7;
const int MAX = 1e5 + 5;
#define fastio                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);
#define int long long int
#define pb push_back
#define d(x) cout << x << "\\n"

// basic mathematics
int gcd(int a, int b) {
  if (a == 0)
    return b;
  return gcd(b % a, a);
}
int lcm(int a, int b) { return (a * b) / gcd(a, b); }

// modular arithmetic
int powermod(int x, int y, int p) {
  int res = 1;

  x = x % p;

  if (x == 0)
    return 0;

  while (y > 0) {
    if (y & 1)
      res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}

// Prime numbers till n precomputation - range query
int prefix[MAX + 1];

void buildPrefix() {
  bool prime[MAX + 1];
  memset(prime, true, sizeof(prime));

  for (int p = 2; p * p <= MAX; p++) {
    if (prime[p] == true) {
      for (int i = p * 2; i <= MAX; i += p)
        prime[i] = false;
    }
  }
  prefix[0] = prefix[1] = 0;
  for (int p = 2; p <= MAX; p++) {
    prefix[p] = prefix[p - 1];
    if (prime[p])
      prefix[p]++;
  }
}

int query(int L, int R) { return prefix[R] - prefix[L - 1]; }

// Find all paths from a to b in an undirected graph
vector<int> g[MAX];
int visited[MAX];
// count of all the paths from 1 to n;

void dfs(int visited[MAX], int u, int v, vector<int> path) {
  visited[u] = 1;
  if (u == v) {
    for (int i = 0; i < path.size(); i++) {
      cout << path[i] << " ";
    }
    cout << "\\n";
  } else {
    for (int i = 0; i < g[u].size(); i++) {
      int y = g[u][i];

      if (visited[y] == 0) {
        visited[y] = 1;
        path.pb(y);
        dfs(visited, y, v, path);
        path.pop_back();
      }
    }
  }

  visited[u] = 0;
}
#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 findProductSum(int A[], int n) {
  // calculating array sum (a1 + a2  ... + an)
  int array_sum = 0;
  for (int i = 0; i < n; i++)
    array_sum = (array_sum + A[i]) % M;

  // calcualting square of array sum
  // (a1 + a2 + ... + an)^2
  int array_sum_square = (array_sum * array_sum) % M;

  // calcualting a1^2 + a2^2 + ... + an^2
  int individual_square_sum = 0;
  for (int i = 0; i < n; i++)
    individual_square_sum += (A[i] * A[i]) % M;

  // required sum is (array_sum_square -
  // individual_square_sum) / 2
  return ((array_sum_square - individual_square_sum % M) / 2) % M;
}

int findGCD(int arr[], int n) {
  int result = arr[0];
  for (int i = 1; i < n; i++) {
    result = gcd(arr[i], result);

    if (result == 1) {
      return 1;
    }
  }
  return result;
}

void solve() {
  int n;
  cin >> n;
  int ar[n];

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

  int yy = findGCD(ar, n);
  int xx = gcd_pairs(ar, n);
  if (xx == (n * (n - 1)) / 2)
    cout << "pairwise coprime"
         << "\\n";
  else if (yy == 1)
    cout << "setwise coprime"
         << "\\n";
  else
    cout << "not coprime"
         << "\\n";
}

int32_t main(void) {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fastio int t = 1;
  // cin >> t;
  while (t--) {
    solve();
  }
  return 0;
}