Please improve test cases so that brute force doesn’t pass.
Totally agree. For this problem only Linear solution should be accepted.