Optimize a function to reduce time complexity from O(n^2) to O(n log n) under specific memory constraints.
You are practicing as a guest. Sign up free to get your answer graded with AI feedback. Your draft stays right here.