I have written the following code but am getting a TLE.Could someone please tell me how to decrease the time complexity.Thanks in advance.

Following is the link of the code :

Next time, please ask a question on editorial page -

Your code complexity is O(n^2)


@betlista I know that the code complexity is O(n^2) but I was asking for a method to decrease it so that I do not get a TLE.

read the editorial :wink:

You have to do it in O(n)