Hi there, I am trying to solve this problem with sparse table, I am getting TLE on this problem.
Here is what I tried.
Thanks in advance.
Your complexity is O(n^2) . But the expected complexity is O(logn) or less.
You can solve this using Stack in O(n)