Given an array of length N Every element of the array will be less than or equal to a given value M.At every level you will be given two integers L and R. your task is to output the answer of the following pseudocode at each level.
ans=0
for i=L to R
for j=L to R
if(a[i]==a[j])
ans=max(ans,j-i)
print ans
Input Format
First line contains 3 integer N,M,Q.
N=Total number of elements in the array.
M is the maximum limit of Ai.
Q is the number of queries to be processed.
Constraints
1<=N,Q,M<=10^5
1<=Ai<=M
1<=L<=R<=N
Output Format
For each query print the required answer in new line.
Sample Input
5 3 3
1 2 3 2 1
1 4
2 5
1 5
Sample Output
2
2
4
Explanation
-
a[2]=2, a[4]=2 ans=4-2=2
-
a[2]=2, a[4]=2 ans= 4-2=2
-
a[1]=1 , a[5]=1 ans=5-1=4
Can someone tell me an efficient approach??
can it be solved with Mo Algorithm?