Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am getting the error of time limit exceeded while executing the code in hackerearth compiler, the code is giving the correct output but while submitting the code the time limit exceeds, please help in this code, is it not optimized.

What I have tried:

#include <stdio.h>

main()
{
    long long int n,q,i,x,y,a[1000001],s;

    scanf("%lld %lld",&n,&q);//no of array elements and querries

    for(i=1;i<=n;i++)
        scanf("%lld",&a[i]);

    while(q--)
    {
        x=0,y=0,s=0;

        scanf("%lld %lld",&x,&y);//upper and lower bound of arrays

        for(i=x;i<=y;i++)
            s = s + a[i];

        s = s/(y-x+1);
        printf("%lld\n",s);
    }
}
Posted
Updated 15-Aug-17 1:26am
Comments
Richard MacCutchan 15-Aug-17 4:30am    
What is hackerearth?
Kornfeld Eliyahu Peter 15-Aug-17 5:21am    
A site that gives you programming challenges and possibly mach jobs to your skills based on the solution...
Richard MacCutchan 15-Aug-17 5:42am    
Thanks.
Richard MacCutchan 15-Aug-17 5:41am    
Thanks to Peter for pointing out the answer. You should post your question on the website that is giving you trouble.
Kornfeld Eliyahu Peter 15-Aug-17 5:47am    
I think the problem is that OP's code is too slow and for that can not solve the challenge. In this case I would say, that we can not help - if OP tries to get a job via the challenge, that helping would be a bit too much cheating IMHO...

As far as I understand you are using an online compiler that times out.

While I don't know for sure while that happens it might be that the compiler is initialising stack variables with default values (as other compilers do too at least with debug builds) which took some time.

You have a very large array of 64-bit integers on the stack occupying 8 million bytes which is close to the default stack size of 8 MB used by most compilers.

You can try to allocate the array on the heap using malloc() after the size has been entered. That is the common method.

If you want to show your skills you should also avoid common beginner mistakes (array start index not zero, main does not return an int) and check user inputs for validity (sizes greater than zero and below a max. value, bounds within array size and upper bound larger than lower).
 
Share this answer
 
Those sites are giving you challenges.
Quote:
is it not optimized.

Optimizing is exactly the object of the challenge. Most strait forward solution is never the right solution.
Your program is optimum if requested 1 sum, but not if you are requested many partial sums.
Said otherwise, the solution is a trade off, you spend time to compute something that will help you to get fast answers.

Your challenge is to find how to do, if we give you the solution, then you fail the challenge.
 
Share this answer
 
We can't help you fix this: it's your challenge, not ours.
But the first thing you need to do is try to find out how long it is taking: if you can;t get that from the site, then import the same code into your own compiler, use the same dataset, and time it.
When you have that as a baseline, you can start working out what improves it, and finding out where the bulk of the time is being taken. There are trivial improvements that are relatively simple, like using pointers instead of array accesses - but the major improvements are likely to come from algorithmic changes. And we can't help you there at all, since we have no idea what the question is asking you to do - plus it wouldn't be a fair challenge if we did give you code for a more efficient way to do the same job.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900