Neighbouring _enemy : https://ide.codingblocks.com/s/226001

it shows wrong answer for some inputs ;can u give me some test cases where it fails.

also please tell me whether my approach is correct or not.
approach is written in the code as comments

@sjduttabbsr2017,
Iterating backwards for each i will make the complexity O(n^2), But we need to do it in O(n).

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.