Why I am getting tle in this in all test cases

Take reference from this code as your logic is correct but formatting is not