GCD of an Increasing Subsequence of Length K (mini version)

HARD · C · 3000ms

As a teaser for the final exam, Farhan gives a small array and asks: among all increasing subsequences of a given length K, what's the largest possible GCD? Read an array of N integers and an integer K. Among all increasing subsequences of length exactly K, print the MAXIMUM possible GCD of the subsequence's elements, or -1 if no increasing subsequence of length K exists. (Hint: for each candidate divisor d from large to small, keep only elements divisible by d, and check if an increasing subsequence of length ≥K exists among them — this is a direct simplified version of the hardest problem from your real contest set.)

Input

Line1: N K. Line2: N integers.

Output

Maximum GCD, or -1.

Constraints

1 ≤ K ≤ N ≤ 500, 1 ≤ a_i ≤ 500

Sample Input

5 3
4 8 6 12 3

Sample Output

4
main.c
Loading editor…

Write C, then Run (custom I/O) or Submit (sample tests). ⌘/Ctrl+Enter submits.

GCD of an Increasing Subsequence of Length K (mini version) · DIU ContestHub