Solution
The problem asks us to find the maximum possible GCD of the remaining numbers after taking any one of them away. Naively trying every single combination will result in a complexity of as there are different ways to take away a number and calculating the GCD will take time per case.
To speed this process up, we can calculate the GCDs of every prefix and suffix. Let and . Then the answer is the maximum of .
Time Complexity:
Time Complexity Proof
Implementation
import java.io.*;import java.util.*;public class Main {public static void main(String[] args) throws IOException {Kattio io = new Kattio();int n = io.nextInt();int[] a = new int[n];for (int i = 0; i < n; i++) { a[i] = io.nextInt(); }
Join the USACO Forum!
Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!