Problem p02642 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>
#define endl "\n"
#define ll long long
#define int long long
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cin.exceptions(cin.failbit);                                                 \
  cout.tie(NULL);
using namespace std;
bool isPrime(int n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;
  for (int i = 5; i * i <= n; i += 6) {
    if ((n % i == 0) || (n % (i + 2) == 0))
      return false;
  }
  return true;
}
int32_t main() {
  fast;
  ll n;
  cin >> n;
  ll a[n];
  set<ll> s;
  map<ll, ll> mp;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    s.insert(a[i]);
    mp[a[i]]++;
  }
  ll c = 0;
  for (ll i = 0; i < n; i++) {

    if (mp[a[i]] > 1) {
      continue;
    }
    ll x = a[i];
    ll raj = 0;
    for (ll j = 1; j <= sqrt(x); j++) {
      if (x % j == 0) {
        if (x / j == j) {
          if (j != x && s.find(j) != s.end()) {
            raj = 1;
            break;
          }
        } else {
          if (j != x && s.find(j) != s.end()) {
            raj = 1;
            break;
          }
          if (x / j != x && s.find(x / j) != s.end()) {
            raj = 1;
            break;
          }
        }
      }
    }
    if (raj == 0)
      c++;
  }
  cout << c;
}


Fixed submission:
#include <bits/stdc++.h>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>
#define endl "\n"
#define ll long long
#define int long long
#define fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);                                                               \
  cin.exceptions(cin.failbit);                                                 \
  cout.tie(NULL);
using namespace std;
bool isPrime(int n) {
  if (n <= 1)
    return false;
  if (n <= 3)
    return true;
  if (n % 2 == 0 || n % 3 == 0)
    return false;
  for (int i = 5; i * i <= n; i += 6) {
    if ((n % i == 0) || (n % (i + 2) == 0))
      return false;
  }
  return true;
}
int32_t main() {
  fast;
  ll n;
  cin >> n;
  ll a[n];
  set<ll> s;
  unordered_map<ll, ll> mp;
  for (ll i = 0; i < n; i++) {
    cin >> a[i];
    s.insert(a[i]);
    mp[a[i]]++;
  }
  ll c = 0;
  for (ll i = 0; i < n; i++) {

    if (mp[a[i]] > 1) {
      continue;
    }
    ll x = a[i];
    ll raj = 0;
    for (ll j = 1; j <= sqrt(x); j++) {
      if (x % j == 0) {
        if (x / j == j) {
          if (j != x && s.find(j) != s.end()) {
            raj = 1;
            break;
          }
        } else {
          if (j != x && s.find(j) != s.end()) {
            raj = 1;
            break;
          }
          if (x / j != x && s.find(x / j) != s.end()) {
            raj = 1;
            break;
          }
        }
      }
    }
    if (raj == 0)
      c++;
  }
  cout << c;
}