Problem p03061 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define FOR(i, a, b) for (ll i = (a); i < (ll)b; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1000000000000000
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
using namespace std;
typedef long long ll;
typedef double db;
typedef string str;
typedef pair<ll, ll> p;
constexpr int MOD = 1000000007;

void print(const std::vector<int> &v) {
  std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; });
  std::cout << std::endl;
}

ll gcd(ll a, ll b) {
  if (a < b)
    gcd(b, a);
  ll r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

// Function to find gcd of array of
// numbers
ll findGCD(vector<ll> arr, ll n) {
  ll result = arr[0];
  for (ll i = 1; i < n; i++)
    result = gcd(arr[i], result);
  return result;
}

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  ll gcd1 = findGCD(A, A.size());
  REP(i, N) A[i] /= gcd1;
  ll onecount = 0;
  REP(i, N) {
    if (A[i] == 1)
      onecount++;
  }
  if (onecount >= 2) {
    cout << gcd1 << endl;
    return 0;
  } else if (onecount == 1) {
    ll tmp;
    vector<ll> B(N);
    REP(i, N) {
      B[i] = A[i];
      if (A[i] == 1) {
        B[i] = A[i == 0 ? i + 1 : i - 1];
      }
    }
    ll gcd2 = findGCD(B, B.size());
    cout << gcd1 * gcd2 << endl;
    return 0;
  }
  sort(ALL(A));
  ll smallest = A[1];
  ll gcd2 = 1;
  for (ll i = smallest; i >= 2; i--) {
    ll flag = 0;
    REP(j, N) {
      if (A[j] % i != 0)
        flag++;
    }
    if (flag == 1) {
      gcd2 = i;
      break;
    }
  }
  cout << gcd1 * gcd2 << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (ll i = 0; i < (ll)n; i++)
#define FOR(i, a, b) for (ll i = (a); i < (ll)b; i++)
#define ALL(obj) (obj).begin(), (obj).end()
#define INF 1000000000000000
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
using namespace std;
typedef long long ll;
typedef double db;
typedef string str;
typedef pair<ll, ll> p;
constexpr int MOD = 1000000007;

void print(const std::vector<int> &v) {
  std::for_each(v.begin(), v.end(), [](int x) { std::cout << x << " "; });
  std::cout << std::endl;
}

ll gcd(ll a, ll b) {
  if (a < b)
    gcd(b, a);
  ll r;
  while ((r = a % b)) {
    a = b;
    b = r;
  }
  return b;
}

// Function to find gcd of array of
// numbers
ll findGCD(vector<ll> arr, ll n) {
  ll result = arr[0];
  for (ll i = 1; i < n; i++)
    result = gcd(arr[i], result);
  return result;
}

int main() {
  ll N;
  cin >> N;
  vector<ll> A(N);
  REP(i, N) cin >> A[i];
  ll gcd1 = findGCD(A, A.size());
  REP(i, N) A[i] /= gcd1;
  ll onecount = 0;
  REP(i, N) {
    if (A[i] == 1)
      onecount++;
  }
  if (onecount >= 2) {
    cout << gcd1 << endl;
    return 0;
  } else if (onecount == 1) {
    ll tmp;
    vector<ll> B(N);
    REP(i, N) {
      B[i] = A[i];
      if (A[i] == 1) {
        B[i] = A[i == 0 ? i + 1 : i - 1];
      }
    }
    ll gcd2 = findGCD(B, B.size());
    cout << gcd1 * gcd2 << endl;
    return 0;
  }
  sort(ALL(A));
  ll smallest = A[1];
  ll gcd2 = 1;
  for (ll i = smallest; i >= 2; i--) {
    ll flag = 0;
    REP(j, N) {
      if (A[j] % i != 0)
        flag++;
      if (flag >= 2)
        break;
    }
    if (flag == 1) {
      gcd2 = i;
      break;
    }
  }
  cout << gcd1 * gcd2 << endl;
  return 0;
}