Problem p02574 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define ll unsigned long long int

bool isPrime(int n) {
  int start = sqrt(n);
  for (int i = start; i > 1; i--)
    if (n % i == 0)
      return false;

  return true;
}
int gcd(int no1, int no2) {
  if (no2 == 0)
    return no1;
  return gcd(no2, no1 % no2);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t;
  t = 1;

  while (t--) {
    int n;
    cin >> n;

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

    map<int, int> exist;
    bool firstSol = true;

    vector<int> nonPrime;
    vector<int> prime;

    for (int i = 0; i < n; i++) {
      if (data[i] != 1) {
        auto it = exist.find(data[i]);
        if (it == exist.end())
          exist.insert(make_pair(data[i], 1));
        else
          firstSol = false;

        if (isPrime(data[i]))
          prime.push_back(data[i]);
        else
          nonPrime.push_back(data[i]);
      }
    }

    int finalAns = false;
    if (firstSol) {
      bool allOne = true;
      for (int i = 0; i < prime.size(); i++) {
        for (int j = 0; j < nonPrime.size(); j++) {
          if (nonPrime[j] > prime[i] && (nonPrime[j] % prime[i]) == 0) {
            allOne = false;
            break;
          }
        }
        if (!allOne)
          break;
      }
      if (allOne) {
        for (int i = 0; i < nonPrime.size(); i++) {
          for (int j = i + 1; j < nonPrime.size(); j++) {
            if (gcd(nonPrime[i], nonPrime[j]) != 1) {
              allOne = false;
              break;
            }
          }
          if (!allOne)
            break;
        }
      }
      if (allOne) {
        finalAns = true;
        cout << "pairwise coprime" << endl;
      }
    }

    if (!finalAns) {
      int ans = data[0];
      for (int i = 1; i < n; i++)
        ans = gcd(ans, data[i]);

      if (ans == 1) {
        finalAns = true;
        cout << "setwise coprime" << endl;
      }
    }
    if (!finalAns)
      cout << "not coprime" << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll unsigned long long int

bool isPrime(int n) {
  int start = sqrt(n);
  for (int i = start; i > 1; i--)
    if (n % i == 0)
      return false;

  return true;
}
int gcd(int no1, int no2) {
  if (no2 == 0)
    return no1;
  return gcd(no2, no1 % no2);
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t;
  t = 1;

  while (t--) {
    int n;
    cin >> n;

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

    map<int, int> exist;
    bool firstSol = true;

    vector<int> nonPrime;

    for (int i = 0; i < n; i++) {
      if (data[i] != 1) {
        auto it = exist.find(data[i]);
        if (it == exist.end())
          exist.insert(make_pair(data[i], 1));
        else
          firstSol = false;

        // if(!isPrime(data[i]))
        nonPrime.push_back(data[i]);
      }
    }

    int finalAns = false;
    if (firstSol) {
      bool allOne = true;
      for (int i = 0; i < nonPrime.size(); i++) {
        for (int j = i + 1; j < nonPrime.size(); j++) {
          if (gcd(nonPrime[i], nonPrime[j]) != 1) {
            allOne = false;
            break;
          }
        }
        if (!allOne)
          break;
      }
      if (allOne) {
        finalAns = true;
        cout << "pairwise coprime" << endl;
      }
    }

    if (!finalAns) {
      int ans = data[0];
      for (int i = 1; i < n; i++)
        ans = gcd(ans, data[i]);

      if (ans == 1) {
        finalAns = true;
        cout << "setwise coprime" << endl;
      }
    }
    if (!finalAns)
      cout << "not coprime" << endl;
  }
  return 0;
}