# Help needed in getting A/C in frequent values problem from spoj?

Problem link :- FREQUENT - Frequent values

This is My Solution, I am getting TLE even after using fast I/o, anyone please help me in getting TLE in this problem. The problem is based on segment tree.

@vivek_1998299

@vivek_1998299

My approach :-

For any query if a[L]=a[R], then answer will simply be R-L+1.

if a[L]!=a[R] then and elements at L AND/OR R spill outside the segment then answer will be

``````max{
count of element at L inside the range,
count of element at R inside the range,
Maximum frequency excluding elements at both the
}
``````

Suppose elements are 2 2 2 2 3 3 3 4 4 4 4 4 then frequency array will be, 4 4 4 4 3 3 3 5 5 5 5 5 and range is [3,8] here some 4 and some 5 are outside the range answer will be

``````max{
no. of 2 inside [3,8] which is 2
no. of 4 inside [3,8] which is 2
maximum frequency count in range [5,7] 3
}
``````

In my solution each node of the segment tree represents the maximum frequency count in that range.

I hope this make sense, ask me if still have doubts.