Test case 7 and test case 8 is giving TLE

please solve my query

@Nidhi_Alipuria
One optimization can be passing the parameters like ans by reference in functions.
Otherwise it will always take extra time for copy the passed values from main function to called function. But it may still give TLE.

Also don’t use that check() function, it is causing extra time, due to which TLE is there. Do without using that.
I will suggest you to use set ans, so you will not need to sort it and not even check it.

I hope it clears your doubt. Feel free to ask!
If you understood it now then please mark this doubt as resolved.