#K67742. Minimum Distances

    ID: 32710 Type: Default 1000ms 256MiB

Minimum Distances

Minimum Distances

You are given an array of integers. Your task is to find the minimum distance between any two identical elements in the array. The distance between two elements is defined as the absolute difference of their indices. If no such pair exists, output -1.

More formally, given an array \( a_1, a_2, \dots, a_n \), you need to find the minimum value of \( |i - j| \) such that \( a_i = a_j \) and \( i \neq j \). If no such indices exist, print \(-1\).

Example:

For the input array [3, 2, 1, 2, 3], the minimum distance is 2 because the second occurrence of 3 is at index 5 and the first is at index 1 (difference 4), and for 2 the indices are 2 and 4 (difference 2). Hence, the answer is 2.

inputFormat

The first line of input contains a single integer \( n \) (the number of elements in the array). The second line contains \( n \) space-separated integers representing the array elements.

\( 1 \leq n \leq 10^5 \). Each element can be any integer within a reasonable range.

outputFormat

Output a single integer which is the minimum distance between any two equal elements. If no such pair exists, output -1.

## sample
5
3 2 1 2 3
2

</p>