QUESTION DOUBT . . .

When i first read the problem statement of this question, then the approach that comes in my mind is brute force approach, but when i see the editorial of this problem, then i cam to know about the more better methods for solving this problem.

So, i want to ask you that how can we think of best approaches of the problem???