Help needed CHANDF

I’m trying to do exactly what Rachit Jain sir [https://youtu.be/-F7cHQ-gWS4] has described in his tutorial,and tested with all possible test cases present in the discussion forum and got correct answers but still getting TLE.Can someone please point out what i am missing.Thanks.
Here is the link:
(https://www.codechef.com/viewsolution/33108198)