The problem is like this. There are n players. Two parameters, (coordination C and problem solving skills Ps) of each player are given. We have to select a team of two players, such that the absolute difference between coordination of both players, is less than a value (k, which is already given) and the sum of their problem solving skills is and absolute difference between coordination of both players is maximum.

i.e. abs(c1-c2) <=k

And

Maximize - ps1 + ps2 + abs(c1-c2).

I have tried this naively using two for loops with O(n^2) complexity. Is there any way we can optimize this?