ADADET - WA

Can anyone help me with the WA test cases of ADADET problem of June Cookoff 2017? More specifically which conditions am I missing?

Here is my solution: CodeChef: Practical coding for everyone

There are multiple test cases, you are outputing the last -1 at the wrong place. Secondly, your solution can TLE because it’s O(n^2), but the problem states n \leq 10^5. Try optimizing your solution first.

Same Here. I Tried My Code on many different test-cases , all passing but on submission WA.
Link to my Submission : CodeChef: Practical coding for everyone