#K54237. Longest Unique Contiguous Subarray Query
Longest Unique Contiguous Subarray Query
Longest Unique Contiguous Subarray Query
You are given an array A consisting of n integers and q queries. For each query, given two integers l and r (1-indexed), you need to compute the length of the longest contiguous subarray within the range \([l, r]\) in which all elements are unique.
More formally, for each query, find the maximum integer \(L\) such that there exist indices \(i\) and \(j\) with \(l \le i \le j \le r\) satisfying \(j - i + 1 = L\) and all elements \(A[i], A[i+1], \dots, A[j]\) are distinct. That is, find \[ L = \max_{l \le i \le j \le r} \{j - i + 1 \;:\; \text{all } A[k] \text{ for } i \le k \le j \text{ are distinct} \}. \]
It is guaranteed that the input will follow the format described below. You need to output one integer per query, each on a new line.
inputFormat
The first line contains an integer n denoting the number of elements in the array.
The second line contains n space-separated integers representing the elements of the array.
The third line contains an integer q denoting the number of queries.
Each of the next q lines contains two space-separated integers l and r representing a query.
Note: The array indexing is 1-based.
outputFormat
For each query, output a single integer on a new line denoting the length of the longest contiguous subarray (within the given range) that contains only unique elements.
## sample7
1 2 1 3 4 2 5
3
1 3
2 7
4 6
2
4
3
</p>