Why do I get a Time Limit Exceeded?

,

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

There’s an out-of-bounds access in the sample testcase:

[simon@simon-laptop][08:30:16]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling j_o_s_h_101-CRICRANK.cpp
+ g++ -std=c++14 j_o_s_h_101-CRICRANK.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv -fno-sanitize-recover
+ set +x
Successful
[simon@simon-laptop][08:31:00]
[~/devel/hackerrank/otherpeoples]>echo "3
0 1 2
2 3 4
10 10 10
8 8 8
10 0 10
0 10 0
" | ./a.out
j_o_s_h_101-CRICRANK.cpp:12:24: runtime error: index 4 out of bounds for type 'int [3]'

Fix that first :slight_smile:

If you need a hint, look very closely at:

j=0;i<3;j++

can anyone tell something for fast i/o in javascript ?

pysco no longer works for python

checkout this plz

Hey @jyot_150 :wave: ,
I would recommend you to always take input like the way I did it is safe.

<?php
$result = trim(fgets(STDIN));

list($a,$b) = explode(' ',$result);

$sub = $a - $b;
echo $sub;
?>
for i in range(int(input())):
    n,k=map(int,input().split())
    while n>=k:
        n-=k
    print(n)

Why am I getting a TLE on this? Problem code: SMOL

Hey @ilvpython !
The problem of TLE occurs when the time that your program runs for exceeds the limit that was set for that particular question.
For instance in this program, if the value of n is very high (1≤N≤10**9) and k is 1 then it would obviously exceed the set time limit.
To avoid this u can just use remainder division to get the answer for this qn.
Also, we must take care of ZeroDivisionError and if the value of the dividend is smaller than the divisor as well.


for i in range(int(input())):
n,k=map(int,input().split())
if n>=k and k!=0:
a = n%k
print(a)
else:
print(n)


1 Like