In editorial O(nlon) solution is given
Please provide O(n) solution
This code is producing TLE for last test case as it involves sorting and it causing TLE…Please explain this code with some input 
@mb129162 hey check this: https://ide.codingblocks.com/s/308231
I think this will work and please dry run it with given sample test case you will get it.
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.