Search code examples
ctimeouttime-complexityterminate

My submission is not accepted due to Timeout in Hackerrank


I got a "Terminated due to timeout error" when I ran my code for some specific testcases only. Even though my code compiled successfully for other testcases. Can someone please help me with this?

Problem Statement
A number which can be exactly divided by 2 is called an even number. Continue is a loop control statement opposite to break statement, instead of terminating the loop, it forces to execute the next iteration of the loop. Sohan is curious to know the total even numbers in a given range but his friend don't like a number .

Input Format

First line will contain , number of testcases. Then the testcases follow: Each testcase contains of a single line containing three numbers & .

Constraints
1<=T<=10^5
0<=a,b,k<=10^8

Output Format

For each testcase, output a single integer, total even count in range except .

Sample Input 0

2
2 5 4
0 8 5

Sample Output 0

1
5

Explanation 0

In the 1st test case: 2 is only even except 4 . In the 2nd test case: 0,2,4,6,8

My Answer is:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() 
{
  int t;
  scanf("%d",&t);
  while(t--)
   {
     int a,b,k,i,count=0;
     scanf("%d %d %d",&a,&b,&k);
     for(i=a;i<=b;i++)
       {
         if(i%2==0)
           {
             if(i==k)
               continue;
             else
               count+=1;
            }    
        }
    printf("%d\n",count);
    }
  return 0;
}

Solution

  • Your code works correctly, however it can be sped up. The complexity of your algorithm if O(n) which means that, in the worst case, it will loop over 10^5 * 10^8 = 10^13 numbers. As a general rule of thumb, the maximum number of operations your algorithm is allowed to perform in these kinds of questions is 10^7, so you are way over the limit.

    This means you need to find a better algorithm which is able to calculate the result without having to loop through all of the numbers. Try finding the answer to these cases by yourself (without using your program!), and you should notice a pattern which lets you find the algorithm. Notice that I haven't mentioned k - once you have an algorithm it is easy to add it as an afterthought.

    • a = 0, b = 100
    • a = 0, b = 10000
    • a = 3, b = 10000
    • a = 100, b = 200
    • a = 101, b = 20005

    I've described the outline of an algorithm below but I encourage you to try to figure the question out yourself before you reveal the spoiler.

    You only care about even numbers. This means when a is odd, we can increase it by 1, and when b is odd, we can decrease it by 1, all without changing the result. This simplifies the rest of the algorithm by letting us think of even numbers only. There are exactly b - a + 1 integers between a and b inclusive, and exactly (b - a) / 2 + 1 of these are even (half, rounding up). If k is odd, or outside of the range a <= k <= b, we can safely ignore it. If k is even and between a and b, we simply need to subtract one from the result.