#K56657. Smallest Subarray Length
Smallest Subarray Length
Smallest Subarray Length
Given an array of n integers, find the length of the smallest contiguous subarray that contains every distinct element present in the entire array. Formally, if the array is represented as \(a_0, a_1, \dots, a_{n-1}\) and the set of unique elements is \(U\), then you need to find the minimum length \(L = j - i + 1\) such that the subarray \(a_i, a_{i+1}, \dots, a_j\) satisfies \(U \subseteq \{a_i, a_{i+1}, \dots, a_j\}\). Note that if n is 0, output 0.
A common approach to solve this problem is to use a sliding window (two pointers) method which dynamically adjusts the window to maintain all unique elements while trying to minimize its size.
inputFormat
The input is read from standard input (stdin) and has the following format:
- The first line contains an integer n, the number of elements in the array.
- The second line contains n space-separated integers representing the elements of the array.
outputFormat
Output a single integer to standard output (stdout) — the length of the smallest contiguous subarray that contains all the distinct elements of the input array.
## sample5
2 1 2 3 3
3
</p>