Max query-i tle

since i have made tree[] array containing the count of max element in the range and then querying so the tle is shown any other approach i am not able to figure out ps help

for my solution

The first-most thing you need to understand is that the value of K is different for every query. Hence you simply cannot build the tree every-time which will consume a lot of time. Hence think in terms of solving this problem offline using Fenwick trees.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.

If this problem can’t be solved by just using segment trees. Is it necessary to use fenwick trees?
Here’s my code, even though I’m not building seg tree again & again still I’m getting TLE.