Only one test case is correct

Please check my code. I am only getting one test case correct and getting TLE on many cases and wrong answer in one test case

Link: https://ide.codingblocks.com/s/269337

Your code follows n^2 time complexity that is why it shows tle on many cases. Try to find a optimised logic for the given code in question so that it produces same output in more efficient way.