PLAYFIT :Time limit Exceeded in c++

I have not used nested loops (i,j) used only one STL and one for loop then why compiler gives TLE…

Do you know that max_element takes linear time?


Hey please use formatted code with good indentation as during debugging indentation makes it faster to debug buggy code.

To answer the question why you are having TLE is that you are using max_element and that takes linear time to find the max element as it needs to traverse the whole length. Now you are using max_element within a loop from i=1 to i=n-1. So it is an overall n^2 algorithm. So it will give TLE as per the constraints specified in the questions!


Thank you so much