Not able to clear 3rd subtask

CodeChef: Practical coding for everyone :
Link to my code.
Successfully cleared first 2 subtasks but unable to clear the 3rd one.

Thanks.

Try to optimize , ur code runs in O(N2) complexity