Please help me to find out why my code is giving tle for 3 test cases?

Code -https://ide.codingblocks.com/s/246395

look at the constraints

Constraints
0< n< 10^5

your approach time complexity is O(n^2)
which will definitely give TLE

try to do it in O(nlogn)

Help me think how to improve the complexity.