module ABC125

  ! <module>s to import
  use, intrinsic :: iso_fortran_env

  ! require all variables to be explicitly declared
  implicit none

  ! accessibility of <subroutine>s and <function>s in this <module>
  public  :: task_C
  private :: calculate_gcd
  private :: calculate_gcd_vector

  ! contained <subroutine>s and <function>s are below
  contains

  subroutine task_C

    ! variables for this <function>
    integer(INT32) :: num_integers
    integer(INT32) :: gcd_fore
    integer(INT32) :: gcd_rear

    ! array for this <function>
    integer(INT32), allocatable :: list_integer(:)

    ! STEP.01
    ! read out the number of given integers
    read(unit=INPUT_UNIT, fmt=*) num_integers

    ! STEP.02
    ! allocate the array to store the given integers
    allocate( list_integer(1:num_integers) )

    ! STEP.03
    ! read out the given integers
    read(unit=INPUT_UNIT, fmt=*) list_integer(1:num_integers)

    ! STEP.04
    ! calcualte the greatest common divisors
    gcd_fore = calculate_gcd_vector(list_integer(:), 1_INT32,                          num_integers / 2_INT32 + 1_INT32)
    gcd_rear = calculate_gcd_vector(list_integer(:), num_integers / 2_INT32 + 1_INT32, num_integers                    )

    ! STEP.05
    ! output the greatest common divisors
    write(unit=OUTPUT_UNIT, fmt='(I0)', advance='yes') max(gcd_fore, gcd_rear)

    ! STEP.06
    ! deallocate the array to store the given integers
    deallocate( list_integer )

    ! STEP.END
    return

  end subroutine task_C

  recursive function calculate_gcd (x, y) result(gcd)

    ! arguments for this <function>
    integer(INT32), intent(in) :: x, y

    ! return value of this <function>
    integer(INT32) :: gcd

    if (y .eq. 0_INT32) then
      gcd = x
    else
      gcd = calculate_gcd(y, mod(x, y))
    end if

    return

  end function calculate_gcd

  function calculate_gcd_vector (array, begin, end) result(gcd)

    ! arguments for this <function>
    integer(INT32), intent(in) :: array(:)
    integer(INT32), intent(in) :: begin, end

    ! return value of this <function>
    integer(INT32) :: gcd

    ! support variables for this <function>
    integer(INT32) :: itr

    gcd = array(begin)

    do itr = begin+1_INT32, end, 1_INT32
      gcd = calculate_gcd(array(itr), gcd)
    end do

    return

  end function calculate_gcd_vector

end module ABC125


program main

  ! <module>s to import
  use, non_intrinsic :: ABC125

  ! require all variables to be explicitly declared
  implicit none

  call task_C

end program main