Can not understand the reason for TLE

I can’t understand why my code gives TLE for the second test case.

Here is link to my submission: Solution: 67775958 | CodeChef

TLE seems to be due to slow I/O process as Input Data might be large.
To overcome this just add the following two lines before taking your input for the testcases:-

ios::sync_with_stdio(0);
cin.tie(0);

Modified (AC) code of yours can be found here.
For more information on this, visit here.
Also, It is recommended to use cout << '\n'; instead of cout << endl; as endl is slower because it forces a flushing stream, which is usually unnecessary for non-interactive problems.

1 Like

Thanks a lot @ayushman_25. :smiley: