lundi 25 janvier 2021

Time limit Exceeded in if-else

Following is a function which change the value of array according to conditions : 1.If the difference between the array element and the next multiple of 5 is less than 3, round up to the next multiple of 5. 2 If the value of array element is less than 38, no rounding occurs. Code :

vector gradingStudents(vector grades) {

int n = grades.size();
int i = 0;

while(i<n)
{
    if(grades.at(i)<38) 
    {
        i++;
    }
    else if( (grades.at(i)+1) % 5==0 ) 
    {
       grades.at(i) += 1;
       i++;
    }
    else if( (grades.at(i)+2) % 5 == 0) 
    {
        grades.at(i) += 2 ;
        i++;
    }
}

return grades;

}

Hackerrank site saying " Time Limit Exceeded". I dont understand how can time limit exceeded when there is no loop (except the essential while loop). If I remove i++ from each if statement and put it outside all if statements then its working fine, but the no. of statements remain same. please help me out

Aucun commentaire:

Enregistrer un commentaire