#K70242. Longest Unique Subarray
Longest Unique Subarray
Longest Unique Subarray
You are given an array of integers. Your task is to find the length of the longest contiguous subarray that contains all unique elements.
In other words, find the maximum size of a subarray in which no element appears more than once. Formally, if the subarray is represented by indices \(i\) to \(j\) (inclusive), then all elements \(a_i, a_{i+1}, \ldots, a_j\) must be distinct. The answer is the maximum value of \(j-i+1\) over all such subarrays.
Hint: You can solve this problem using a sliding window technique. The key idea is to maintain a window and adjust its left boundary when a duplicate element is encountered.
For example, for the input array [2, 1, 2, 3, 4, 1], the longest subarray with all unique elements is [1, 2, 3, 4] and its length is 4.
inputFormat
The input is given via standard input (stdin) and consists of two lines:
- The first line contains an integer \(n\) (\(0 \le n \le 10^5\)), representing the number of elements in the array.
- If \(n > 0\), the second line contains \(n\) space-separated integers representing the elements of the array.
outputFormat
Output a single integer to standard output (stdout), which is the length of the longest contiguous subarray composed solely of unique elements.
## sample6
2 1 2 3 4 1
4
</p>