Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/minmax-39-max-va-min-eng-yaqin-masofa

Shortest distance between max and min

Medium 1000 ms 128 MB 62%
Share

A positive integer $N$ and $N$ integers are given. Among all pairs $i$ and $j$ where $A_{i}$ equals the largest value and $A_{j}$ equals the smallest value, find the smallest value of $|i - j|$. If all elements are equal, the answer is $0$.

The closest pair of elements equal to the largest and the smallest values

Input

The first line contains a positive integer $N$. The next line contains $N$ integers $A_{i}$.

Output

One integer.

Note

$1 \le N \le 10^{5}$
$-10^{9} \le A_{i} \le 10^{9}$

Examples

Input #1
8
9 2 5 9 7 1 4 9
Output #1
2
Input #2
3
6 6 6
Output #2
0

Sign in to submit a solution

Signing up is free and takes a minute.