#K4576. Longest Subarray with All Distinct Elements
Longest Subarray with All Distinct Elements
Longest Subarray with All Distinct Elements
You are given an array of N integers. Your task is to find the length of the longest contiguous subarray in which all the elements are distinct.
For example, if the array is [1, 2, 3, 4, 5], then every subarray has distinct elements, and the answer is 5. However, for the array [1, 2, 2, 3, 4, 4, 5], the longest contiguous subarray with all distinct elements is either [2, 3, 4] or [3, 4, 5] with a length of 3.
The required solution should efficiently handle arrays up to large sizes (for example, 100,000 elements) using appropriate data structures. Mathematics behind the sliding-window technique can be expressed in LaTeX as follows:
\( \text{max_length} = \max_{0 \leq i \leq N} \{ i - \text{start} + 1 \} \)
where start is updated each time a repeated element is encountered.
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 elements of the array.
outputFormat
Output a single integer representing the length of the longest contiguous subarray that contains all distinct elements.
## sample5
1 2 3 4 5
5