Language: C++

#include <bits/stdc++.h>

using namespace std;

template <class M, class N>
common_type_t<M, N> gcd(M a, N b) {
  if (b == 0) {
    return a;
  } else {
    return gcd(b, a % b);
  }
}

int main(int argc, const char *argv[]) {
  int n;
  cin >> n;
  vector<int> va(n);
  for (int i = 0; i < n; ++i) {
    cin >> va[i];
  }

  // 0番目の要素を除く場合の最大公約数
  // 全ての要素の最大公約数
  vector<int> gcds(n), rev_gcds(n);
  gcds[0] = va.front();
  rev_gcds[0] = va.back();
  for (int i = 1; i < va.size(); ++i) {
    gcds[i] = gcd(gcds[i - 1], va[i]);
    rev_gcds[i] = gcd(rev_gcds[i - 1], va[n - i - 1]);
  }

  auto except_gcd = [&](int idx) {
    if (idx == 0) {
      return rev_gcds[n - 2];
    } else if (idx == n - 1) {
      return gcds[n - 2];
    } else {
      return gcd(gcds[idx - 1], rev_gcds[n - idx - 2]);
    }
  };

  int ans = except_gcd(0);
  for (int i = 0; i < n; ++i) {
    ans = max(ans, except_gcd(i));
  }

  cout << ans << '\n';

  return 0;
}

Compiler output:
a.cc: In function 'int main(int, const char**)':
a.cc:28:18: error: call of overloaded 'gcd(__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type&, __gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type&)' is ambiguous
   28 |     gcds[i] = gcd(gcds[i - 1], va[i]);
      |               ~~~^~~~~~~~~~~~~~~~~~~~
a.cc:6:21: note: candidate: 'std::common_type_t<_Mn, _Nn> gcd(M, N) [with M = int; N = int; std::common_type_t<_Mn, _Nn> = int]'
    6 | common_type_t<M, N> gcd(M a, N b) {
      |                     ^~~
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:58,
                 from a.cc:1:
/usr/include/c++/14/numeric:173:5: note: candidate: 'constexpr std::common_type_t<_Mn, _Nn> std::gcd(_Mn, _Nn) [with _Mn = int; _Nn = int; common_type_t<_Mn, _Nn> = int]'
  173 |     gcd(_Mn __m, _Nn __n) noexcept
      |     ^~~
a.cc:29:22: error: call of overloaded 'gcd(__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type&, __gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type&)' is ambiguous
   29 |     rev_gcds[i] = gcd(rev_gcds[i - 1], va[n - i - 1]);
      |                   ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:6:21: note: candidate: 'std::common_type_t<_Mn, _Nn> gcd(M, N) [with M = int; N = int; std::common_type_t<_Mn, _Nn> = int]'
    6 | common_type_t<M, N> gcd(M a, N b) {
      |                     ^~~
/usr/include/c++/14/numeric:173:5: note: candidate: 'constexpr std::common_type_t<_Mn, _Nn> std::gcd(_Mn, _Nn) [with _Mn = int; _Nn = int; common_type_t<_Mn, _Nn> = int]'
  173 |     gcd(_Mn __m, _Nn __n) noexcept
      |     ^~~
a.cc: In lambda function:
a.cc:38:17: error: call of overloaded 'gcd(__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type&, __gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type&)' is ambiguous
   38 |       return gcd(gcds[idx - 1], rev_gcds[n - idx - 2]);
      |              ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:6:21: note: candidate: 'std::common_type_t<_Mn, _Nn> gcd(M, N) [with M = int; N = int; std::common_type_t<_Mn, _Nn> = int]'
    6 | common_type_t<M, N> gcd(M a, N b) {
      |                     ^~~
/usr/include/c++/14/numeric:173:5: note: candidate: 'constexpr std::common_type_t<_Mn, _Nn> std::gcd(_Mn, _Nn) [with _Mn = int; _Nn = int; common_type_t<_Mn, _Nn> = int]'
  173 |     gcd(_Mn __m, _Nn __n) noexcept
      |     ^~~