Why do I get a Time Limit Exceeded?

,

Why do i get time limit error

t=int(input())
while(t>0):
t=t-1
m,n=map(int,input().split())
if m==1:
m=2
for i in range(m,n+1):
c=0
for j in range(2,i//2+1):
if (i%j==0):
c=1
break
if(c<=0):
print(i)
print()

I’m unable to solve TLE issue in my code.Please guide me.
Question Link:PRIME1 Problem - CodeChef

Learn other method method to find primes - seive of ertoshthenis.
And also learn about time complexity and time limits.

2 Likes

Thanks for guiding:)

1 Like

Can someone send the link for Enormous Input Test problem

#include
#include<stdio.h>
using namespace std;

int main() {
int t=0;
scanf("%d",&t);
while(t–)
{
unsigned long long n=0,c=0;
scanf("%u",&n);

    for(unsigned long long i=1;i<=n;i++)
    {
        if(i%2==0)
        {
        if(n%2==0)
        {
            i=i/2;n=n/2;
        }
        else
        c++;
        }
        
    }
    printf("%u",c);
    printf("\n");
    
}
return 0;

}
why i am gey
ting tle

def adjacentElementsProduct(arr):

p=0

for i in range(len(arr)) :

    if i+1 < len(arr) :

        if arr[i]*arr[i+1] > p:

            p=arr[i]*arr[i+1]

return p

arr= list(map(int,input().split()))

adjacentElementsProduct(arr)

showing TLE what is the solution

please post question link.

is it a ongoing contest, it shows me “not authorised” error ?

not a contest it is a site similar to hackerrank “codesignal” is its name.
Given an array of integers, find the pair of adjacent elements that has the largest product and return that product.

Example

For inputArray = [3, 6, -2, -5, 7, 3] , the output should be
adjacentElementsProduct(inputArray) = 21 .

7 and 3 produce the largest product.

this is the question

Here is my Accepted solution …

def adjacentElementsProduct(inputArray):

mas=inputArray[0]*inputArray[1]

for i in range(1,len(inputArray)-1):

    mas=max(mas,inputArray[i]*inputArray[i+1])

return mas

I am new to code chef. I am getting time limit exceeded error. I had learned to remove constraints and removed them as well. I have tried two solutions.
this is the question - ZUBSPOIL Problem - CodeChef
this is the first answer - CodeChef: Practical coding for everyone
this is the second answer - CodeChef: Practical coding for everyone
Python 3.6
I couldn’t able to understand why time limit exceeded on submission. For runcode it is working with sample inputs.

It takes 10.1 sec on submission. I read the form and included this two lines of code in my answer:
import psyco
psyco.full
Then it takes time taken is 0.01 sec with runtime error ncez.

And also how to trace the runtime error ncez. I couldn’t able to see whats the error.

Dude, if you’re getting TLE, it should be one of the two following reasons ( sometimes, both).

  1. Your code is not efficient: When your code is not efficient, I mean, if your Code’s time Complexity is huge, you will get TLE. You need to optimise your code to make it run with in Time Limits.
  2. TLE due to poor handling of IO: Usually, CPP Users use cin >> and cout << Statements to handle IO. Similarly, java users use Scanner, Python users use input() and print statements. These are not recommended for Large Input Data. In every language, there is a way to handle IO faster. Try googling Fast IO in Competitive Programming. You’ll learn some interesting and useful stuff.

If you want to avoid Run Time errors, don’t do this. You need to handle Exceptions or Runtime errors using try-catch statements or similar methods which specific languages provide.

There are many kinds of Run-time errors that arise when you solve a Problem.

  1. NZEC: Stands for Non-zero exit code.
  2. SIGABRT: When abort is called.
    etc., Google them.

Finally, I would recommend you to solve easier Problems first, specifically, those which are just above your level. Say you are familiar with Arrays. Then solve problems on Arrays that are just above your level. Learn little every day and you’ll be definitely improve.
Happy Coding.

PS: The Problem you were trying to solve is Categorised under Hard, which is the reason why I had to include the last Paragraph.

If you do this, then you get WA instead of RTE . Not much useful.

2 Likes

You might want to look into this concept:
ios_base::sync_with_stdio(false);cin.tie(0);

Well, I actually meant if he want’s to find the reason for the occurrence of Exception, he/she can use try-catch :joy:

But, actually yes, it results in WA.

#include

using namespace std;

void turboSort(int array[], int t);

int main(){
int t, i;
cin >> t;

int array[t];

for(i = 0; i < t; ++i){
	cin >> array[i];	
}

turboSort(array, t);

return 0;

}

void turboSort(int array[], int t){
int temp;
for(int i = 0; i < (t - 1); ++i){
for (int j = 0; j < (t - i - 1); ++j){
if(array[j] > array[j + 1]){
swap(array[j], array[j + 1]);
}
}
}
for(int k = 0; k < t; ++k){
cout << array[k] << endl;
}
}

Why do i get time limit error

Please refer these link :

Can someone help since this is frequently occurring TLE
https://www.codechef.com/viewsolution/49266193