This problem appeared in a Hiring Challenge on Hackerrank and i couldn't solve it in better than $$$O(N^2)$$$ which was giving TLE. Here is the problem below:
Spoiler
I can't think of a solution better than $$$O(N^2)$$$. Any help/hints will be appreciated.