All problems

Problem 514

Degree of an Array

Easy · 10 pts · Array, Hash Table

Problem

The degree of an array is the highest number of times any single value appears in it. Find the length of the shortest block of consecutive elements that has the same degree as the whole array. For each value, record how often it appears and where it first and last appears; among the values with the highest count, the answer is the smallest distance last - first + 1.

Input

The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1].

Output

Print the length of the shortest subarray with the same degree.

Constraints

1 <= N <= 50000; 0 <= A[i] <= 49999

Example input

5
1 2 2 3 1

Example output

2

Explanation

In 1 2 2 3 1, both 1 and 2 appear twice, so the degree is 2. The block 2 2 has degree 2 and length 2, while any block containing both 1s is longer. So the answer is 2.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Shortest Unsorted Continuous Subarray Next: Minimum Difference After Three Changes