The Complete Solution given is not efficient


#1

The complete solution given for this problem runs in O(n^2) while it can be reduced to O(n logn). Here’s my solution :


#2

You are absolutely right, the provided solutions are suboptimal. This standard problem can be solved in O(nlogn) instead of O(n**2)!