Getting TLE in the code

only 1 test case is passing and rest of them getting TLE due to my O(n^2) approach, please tell me how to optimize my approach

Try this: