How to make it more effective?

My code has not worked for all the testcases. What can I do to make it less time complex

hi @astha119btcse21_7380491a239a3b9b,