NTAT - NPCI Hiring Test Question (Hackerearth)

The Test is over now

I tried to solve this question but unable to get the approach in O(nlogn) or O(n) Time complexity.

If anyone has given the test and solved this one OR have any approach to how to solve this problem Please share.

Thank you !