CF BUDDY
← Problems·

484B · Maximum Value

2100 · binary search, math, sortings

Problem: You are given a sequence a consisting of n integers. Find the maximum possible value of aimodaja_i \bmod a_j (integer remainder of ai divided by aj), where 1 ≤ i, j ≤ n and ai ≥ aj.

Input Format: The first line contains integer n — the length of the sequence (1 ≤ n ≤ 2·105).

The second line contains n space-separated integers ai (1 ≤ ai ≤ 106).

Output Format: Print the answer to the problem.

Sample Cases

Case 1

Input

3
3 4 5

Output

2

Similar problems

00:00:00
Loading editor…
Welcome! I'm your coding tutor for this problem. Use the chips below to reveal stored hints or get AI feedback on your code. I'll guide you step by step — never giving away the solution.

Sign in to unlock AI tutor feedback