308. Smallest Window Holding Everything
Print the length of the shortest contiguous stretch that contains every distinct value present in the whole array.
1 2 2 3 1 -> 3
The array holds three distinct values, and 2 3 1 is the shortest stretch
containing all of them.
Count the distinct values first, so you know what "everything" means. Then grow a window on the right, and whenever it holds them all, record its length and shrink from the left for as long as it still holds them all.
Every element enters and leaves the window once, so despite the nested loop the whole thing is linear.
Constraints - `1 ≤ n ≤ 200000` - `-1000000000 ≤ a[i] ≤ 1000000000`
Input
The first line contains an integer n.
The second line contains n integers.
Output
Print one integer, the length of the shortest stretch containing every distinct value.