Maximize Modulo

0

0 votes
Easy-Medium
Problem

You are given a array A of size N. Lets see if you can find the maximum value of Ai % Aj such that Aj <= Ai and 1 <= i,j <= N.

Input
The first line contains integer N — the length of the sequence.
The second line contains N space-separated integers Ai

Output
Print the answer to the problem in one line

Constrains
1 <= N <= 5*105
1 <= ai <= 106

Sample Input
3
3 4 5
Sample Output
2
Time Limit: 1
Memory Limit: 256
Source Limit:
Editor Image

?