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

#include <bits/stdc++.h>

using namespace std;

#define endl "\\n"

bool prime(long long int n) {
  if (n == 2)
    return 1;

  else if (n % 2 == 0 || n < 2)
    return 0;

  else {
    long long int i;
    for (i = 3; i <= sqrt(n); i += 2) {
      if (n % i == 0)
        return 0;
    }

    return 1;
  }
}

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

  long long int n, f = 0;
  cin >> n;
  long long int a[n], i, g = 0;
  map<long long int, long long int> me, mo, mp;

  for (i = 0; i < n; ++i) {
    cin >> a[i];
  }

  g = a[0];
  for (i = 1; i < n; ++i) {
    g = __gcd(g, a[i]);
  }

  if (a[n - 1] % 2 == 0)
    me[a[n - 1]]++;

  else {
    if (prime(a[n - 1]))
      mp[a[n - 1]]++;

    else
      mo[a[n - 1]]++;
  }

  for (i = n - 2; i >= 0; --i) {
    if (a[i] % 2 == 0) {
      if ((int)me.size() > 0)
        break;

      else {
        if ((int)mo.size() > 0) {
          for (auto it = mo.begin(); it != mo.end(); ++it) {
            if (__gcd(a[i], it->first) != 1) {
              f = 1;
              break;
            }
          }
        }

        if (f)
          break;
      }

      me[a[i]]++;
    }

    else {
      if (prime(a[i])) {
        if ((int)mo.size() > 0) {
          for (auto it = mo.begin(); it != mo.end(); ++it) {
            if (__gcd(a[i], it->first) != 1) {
              f = 1;
              break;
            }
          }
        }

        if (f)
          break;

        mp[a[i]]++;
      }

      else {
        if ((int)mo.size() == 0) {
          if ((int)me.size() > 0) {
            for (auto it = me.begin(); it != me.end(); ++it) {
              if (__gcd(a[i], it->first) != 1) {
                f = 1;
                break;
              }
            }

            if (f)
              break;
          }

          else if ((int)mp.size() > 0) {
            for (auto it = mp.begin(); it != mp.end(); ++it) {
              if (__gcd(a[i], it->first) != 1) {
                f = 1;
                break;
              }
            }

            if (f)
              break;
          }
        }

        else {
          for (auto it = mo.begin(); it != mo.end(); ++it) {
            if (__gcd(a[i], it->first) != 1) {
              f = 1;
              break;
            }
          }

          if (f)
            break;
        }

        mo[a[i]]++;
      }
    }
  }

  if (i >= 0) {
    if (g == 1)
      cout << "setwise coprime";

    else
      cout << "not coprime";
  }

  else {
    cout << "pairwise coprime";
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define endl "\\n"

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

  long long int n, f = 0;
  cin >> n;
  long long int a[n], i, g = 0;
  map<long long int, long long int> me, mo, mp;

  for (i = 0; i < n; ++i) {
    cin >> a[i];
  }

  g = a[0];
  for (i = 1; i < n; ++i) {
    g = __gcd(g, a[i]);
  }

  if (a[n - 1] % 2 == 0)
    me[a[n - 1]]++;

  else {

    mo[a[n - 1]]++;
  }

  for (i = n - 2; i >= 0; --i) {
    if (a[i] % 2 == 0) {
      if ((int)me.size() > 0)
        break;

      else {
        if ((int)mo.size() > 0) {
          for (auto it = mo.begin(); it != mo.end(); ++it) {
            if (__gcd(a[i], it->first) != 1) {
              f = 1;
              break;
            }
          }
        }

        if (f)
          break;
      }

      me[a[i]]++;
    }

    else {

      if ((int)mo.size() == 0) {
        if ((int)me.size() > 0) {
          for (auto it = me.begin(); it != me.end(); ++it) {
            if (__gcd(a[i], it->first) != 1) {
              f = 1;
              break;
            }
          }

          if (f)
            break;
        }
      }

      else {
        for (auto it = mo.begin(); it != mo.end(); ++it) {
          if (__gcd(a[i], it->first) != 1) {
            f = 1;
            break;
          }
        }

        if (f)
          break;
      }

      mo[a[i]]++;
    }
  }

  if (i >= 0) {
    if (g == 1)
      cout << "setwise coprime";

    else
      cout << "not coprime";
  }

  else {
    cout << "pairwise coprime";
  }

  return 0;
}