+ 1

Pls help. This code is showing that time limit exceeds on a particular ide and in some ide it is showing the desired output.

#include<stdio.h> int main() { int T; scanf("%d", &T); for (; T>0; T--) { int d,i,sum=0,count=0; int l,r; scanf("%d\n", &d); scanf("%d %d", &l,&r); for (i=l; i<r;i++) { if (i%2!=0) { count++; if(count>d&&count<=d+3 || count>d+6&&count<=d+9) continue; sum = sum + i; } } printf("%d\n", sum); } return 0; }

28th Feb 2020, 6:26 PM
Aryaman Mishra 🇮🇳
Aryaman Mishra 🇮🇳 - avatar
12 Réponses
+ 6
You can make it faster by moving all variable definitions out of the for loop so the space is only allocated once. Move the d+# out of inner loop so it is only calculated once. Change sum increment to: sum += i; which can be faster.
28th Feb 2020, 7:13 PM
John Wells
John Wells - avatar
+ 6
This is what I suggested. https://code.sololearn.com/c277pnFx6PSX
29th Feb 2020, 4:32 AM
John Wells
John Wells - avatar
+ 4
Dynamic would cost more time over stack.
29th Feb 2020, 3:57 AM
John Wells
John Wells - avatar
+ 4
Put current code in playground and link it here so I can look and play with it.
29th Feb 2020, 3:58 AM
John Wells
John Wells - avatar
+ 1
Can't find any error. Though not what's the desired output seems working fine. What's the compiler where it's failing? Or what's the error?
28th Feb 2020, 6:42 PM
Anurag
Anurag - avatar
+ 1
Thank you.
29th Feb 2020, 1:55 AM
Aryaman Mishra 🇮🇳
Aryaman Mishra 🇮🇳 - avatar
0
Its saying time limit exceeded on codechef. Actually I was doing a contest where time limit for each question was set to be 1 second but my code took 1.01 second and that's where it failed.
28th Feb 2020, 6:45 PM
Aryaman Mishra 🇮🇳
Aryaman Mishra 🇮🇳 - avatar
0
Think that's telling you to improve your code. You can try to improve the code.
28th Feb 2020, 7:01 PM
Anurag
Anurag - avatar
0
John wells sir actually it's still not working. Can we use dynamic memory allocation here to decrease the time of execution?
29th Feb 2020, 3:49 AM
Aryaman Mishra 🇮🇳
Aryaman Mishra 🇮🇳 - avatar
0
Ok
29th Feb 2020, 3:59 AM
Aryaman Mishra 🇮🇳
Aryaman Mishra 🇮🇳 - avatar
29th Feb 2020, 4:02 AM
Aryaman Mishra 🇮🇳
Aryaman Mishra 🇮🇳 - avatar
0
Can you post the task? To see what the code is supposed to do.
29th Feb 2020, 9:01 AM
Rafik Abdelhak Nadir
Rafik Abdelhak Nadir - avatar