is there any other way to avoid TLE or simplify the given code?

i did the below code:

int n, q, l, r;
    scanf("%d %d", &n, &q);
    int a[n], i, j, sum;
    for(i=0;i<n;i++)
    {
        scanf("%d", &a[i]);
    }
    while(q--)
    {
        sum=0;
        scanf("%d %d", &l, &r);
        for(j=l-1;j<=r-1;j++)
        {
            sum+=a[j];
        }
        printf("%d\n", sum);
    }

here n is to take the size of the array and q is the number of test cases. l & r are respectively lower and upper index limit. the goal is to take l & r for each test cases and print the summations of array elements from a[l] to a[r]. is there any other way to simplify the code to avoid TLE?



Read more here: https://stackoverflow.com/questions/65715817/is-there-any-other-way-to-avoid-tle-or-simplify-the-given-code

Content Attribution

This content was originally published by Sanzida Hossain at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: