Getting TLE when using while loop

TLE when while(tc- -) is used


When I submitted my solutions(mostly containing while loops) to some problems I am getting TLE. Whenever I replaced those while loops with the equivalent for loop, my code gets executed successfully and gets an AC.


Here is my code snippet which gets either TLE or some type of Runtime error, when while loops are used.


#include
using namespace std;

int main()
{
#ifndef ONLINE_JUDGE
freopen(“input.txt”, “r”, stdin);
freopen(“output.txt”, “w”, stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tc, n, laddus, rank, severity;
cin >> tc;
string origin, s;
while(tc-- )
{
laddus = 0;
cin >> n >> origin;
while(n-- )
{
cin >> s;
if (s == “CONTEST_WON”) {
cin >> rank;
if (rank <= 20) laddus += 300 + (20 - rank);
else laddus+= 300;
}
else if (s == “TOP_CONTRIBUTOR”) laddus += 300;
else if (s == “BUG_FOUND”) {
cin >> severity;
laddus += severity;
}
else if (s == “CONTEST_HOSTED”) laddus += 50;
}
if (origin == “INDIAN”) cout << laddus / 200 << “\n”;
else if (origin == “NON_INDIAN”) cout << laddus / 400 << “\n”;
}
}

And here is my code snippet when I replaced the while loops with the equivalent for loops and got AC:


#include
using namespace std;

int main()
{
#ifndef ONLINE_JUDGE
freopen(“input.txt”, “r”, stdin);
freopen(“output.txt”, “w”, stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tc, n, laddus, rank, severity;
cin >> tc;
string origin, s;
for (int i = 0; i < tc; i++)
{
laddus = 0;
cin >> n >> origin;
for (int j = 0; j < n; j++)
{
cin >> s;
if (s == “CONTEST_WON”) {
cin >> rank;
laddus += 300;
if (rank <= 20) laddus += 20 - rank;
}
else if (s == “TOP_CONTRIBUTOR”) laddus += 300;
else if (s == “BUG_FOUND”) {
cin >> severity;
laddus += severity;
}
else if (s == “CONTEST_HOSTED”) laddus += 50;
}
if (origin == “INDIAN”) cout << laddus / 200 << “\n”;
else if (origin == “NON_INDIAN”) cout << laddus / 400 << “\n”;
}
}

I want to add that, it is happening with not only this code, but I had the same issue when solving other problems with while and got TLE.


I am wondering why it is happening like this. I don't why this happens or is there any mistake in my code. I would like to know why this happens. Thanks in advance!

You are using a single minus sign in while condition, while(a-) won’t work.
It should be corrected to while(a–), double minus (decrement operator).
Carefully check and update every while statement and it will work in almost same time.

First up, the while statement in your first program, reads while(tc-) and not while(tc–), so you should probably get a Compilation error. If in case, that was rectified, your code shouldn’t have a problem with running time by changing “while” to “for” loop. If the problem prevails, please paste the link to the solution.

Format your code. Use ``` at start and end of code. This will help in reading the code

Sorry, I used – only, but the codechef formatted it as - here in this post. I will change this now. Thanks for pointing out!

Actually, I have used HTML to build the post. So, can I still use ``` in HTML?