Run error, TLE, Wrong answer, only 1 testcase passing

@mudit2jain
Your logic is completely correct after due ananysis of your code,i found out that your code is giving TLE in all cases except one.
As your code is having a Time Complexity of O(n^2) ,try to reduce it to O(nlogn) or O(n).
Fight hard,if you are unable to optimise your code we are here to help you.

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.