Uber Interview Question

One question for coding on coderpad. I answered the brute force solution, which is apparently not what he expected. So we talked about the solution, I did not code in the end. That's the reason that I failed.