#K65427. Longest Unique Subarray Queries
Longest Unique Subarray Queries
Longest Unique Subarray Queries
You are given an array of integers A of length \(N\) and \(K\) queries. Each query is represented by two integers \(L\) and \(R\) (1-indexed) that denote the bounds of a subarray in A.
For each query, your task is to find the length of the longest contiguous subarray within \(A[L \dots R]\) that contains only unique elements (i.e. no duplicates). This is essentially a sliding window problem where you have to adjust the window whenever a duplicate is encountered.
Input Format: The first line contains two integers \(N\) and \(K\). The following line contains \(N\) space-separated integers representing the array. Each of the next \(K\) lines contains two integers \(L\) and \(R\) representing a query.
Output Format: For each query, output a single integer on a new line representing the length of the longest contiguous subarray with all unique elements in the specified range.
inputFormat
The first line contains two space-separated integers \(N\) and \(K\), where \(N\) is the number of elements in the array and \(K\) is the number of queries.
The second line contains \(N\) space-separated integers \(A_1, A_2, \dots, A_N\) representing the array.
Each of the following \(K\) lines contains two space-separated integers \(L\) and \(R\) indicating the 1-indexed bounds of the subarray for that query.
outputFormat
For each query, print a single integer on a new line representing the length of the longest contiguous subarray with all unique elements within the subarray \(A[L \dots R]\).
## sample5 3
1 2 1 3 2
1 5
2 4
2 5
3
3
3
</p>