#C13222. Longest Contiguous Subarray Without Duplicates
Longest Contiguous Subarray Without Duplicates
Longest Contiguous Subarray Without Duplicates
Given an array of integers, your task is to determine the length of the longest contiguous subarray that contains only unique elements (i.e. no duplicates).
Let the array be \(A = [a_1, a_2, \ldots, a_n]\). You need to find the maximum length \(L\) such that there exists an index \(i\) where the subarray \(A[i..i+L-1]\) contains all distinct elements.
For example, if the array is [1, 2, 3, 1, 2, 3, 4, 5], the longest valid subarray is [1, 2, 3, 4, 5] with a length of 5.
If the input array is empty, the output should be 0.
You are expected to design an efficient algorithm (ideally with \(O(n)\) time complexity using a sliding window approach), but any correct solution will be accepted.
inputFormat
The first line of input contains a single integer \(n\) representing the number of elements in the array. If \(n = 0\), the array is empty.
The second line contains \(n\) space-separated integers representing the elements of the array.
outputFormat
Output a single integer, the length of the longest contiguous subarray with no duplicate elements.
## sample5
1 2 3 4 5
5