Home > database >  How to solve "Time Limit Exceeded" error?
How to solve "Time Limit Exceeded" error?

Time:07-12

I am recently practicing on codeforces.com, I solved a problem there:

Kristina has two arrays a and b, each containing n non-negative integers. She can perform the following operation on array a any number of times:

apply a decrement to each non-zero element of the array, that is, replace the value of each element ai such that ai>0 with the value ai−1 (1≤i≤n). If ai was 0, its value does not change. Determine whether Kristina can get an array b from an array a in some number of operations (probably zero). In other words, can she make ai=bi after some number of operations for each 1≤i≤n?

For example, let n=4, a=[3,5,4,1] and b=[1,3,2,0]. In this case, she can apply the operation twice:

after the first application of the operation she gets a=[2,4,3,0]; after the second use of the operation she gets a=[1,3,2,0]. Thus, in two operations, she can get an array b from an array a.

Input The first line of the input contains an integer t (1≤t≤104) —the number of test cases in the test.

The descriptions of the test cases follow.

The first line of each test case contains a single integer n (1≤n≤5⋅104).

The second line of each test case contains exactly n non-negative integers a1,a2,…,an (0≤ai≤109).

The third line of each test case contains exactly n non-negative integers b1,b2,…,bn (0≤bi≤109).

It is guaranteed that the sum of n values over all test cases in the test does not exceed 2⋅105.

Output For each test case, output on a separate line:

YES, if by doing some number of operations it is possible to get an array b from an array a; NO otherwise. You can output YES and NO in any case (for example, strings yEs, yes, Yes and YES will be recognized as a positive response).

here is my solution

#include<stdio.h>
#include<stdlib.h>

void array_decrement(int arr[],int length)   
{
    for(int i=0;i<length;i  )
    {       
        if(arr[i]>0)
        {
            arr[i]--;       //decrements all element by one, if element is 0 then it won't be changed
        }
    }
}

int array_cmp(int arr1[],int arr2[],int length)
{
    for(int i=0;i<length;i  )
    {
        if(arr1[i]<arr2[i])     //if any element will less than element of 2nd array, then it will return -1 
        {
            return -1;
        }
        if(arr1[i]>arr2[i])     //if any element will more than element of 2nd array, then it will return 0
        {
            return 0;
        }
    }
    return 1;                   // if all elements are same then it will return 1
}

int main()
{
    int t,n,cmp;
    scanf("%d",&t);
    for(int i=0;i<t;i  )  // t = number of testcases
    {
        scanf("%d",&n);
        int arr1[n]; 
        int arr2[n];

        for(int p=0;p<n;p  )
        {
            scanf("%d",&arr1[p]);
        }
        for(int p=0;p<n;p  )
        {
            scanf("%d",&arr2[p]);
        }


        // logical part
        while (i>=0)     //loop runs for infinite times, but it has a set of conditions which will break loop in finite time
        {
            if(array_cmp(arr1,arr2,n)==1)          // if all elements are same print "YES"
            {
                printf("YES\n");
                break;

            }else if(array_cmp(arr1,arr2,n)==0)    //if any element can be decremented then it will decrement
            {
                array_decrement(arr1,n);

            }else if(array_cmp(arr1,arr2,n)==-1)    // if any element gets to less than 2nd elemnet during process of decrement then print "NO"
            {
                printf("NO\n");
                break;
            }
        }     
    }
    return 0;
}

My Problem: It shows "Time limit Exceeded" when I went to submit this code, can anyone help me to solve this error?, actually I've faced this error First time.

CodePudding user response:

I am recently practicing on codeforces.com... It shows "Time limit Exceeded" when I went to submit this code.

As paddy says:

These types of coding challenges are designed to test time complexity of the algorithms. When you get a runtime limit exceeded, it's usually because your algorithm is not the best way to solve the problem.

Your algorithm compares the arrays two times in a loop which actually performs all the possible decrements.

Given N, the number of elements in each array (up to 5*104, apparently), and K, the number of possible steps necessary to transform an array into the other (note that 0 <= a[i], b[i] <= 109), the time complexity of the posted algorithm is O(N * K).


Consider the following points:

  • The array a can be transformed into b (applying the rules of the problem statement) if there exits a non-negative value k (the number of decrements) such that, for every i:

    • If b[i] == 0, then k >= a[i].
    • Otherwise:
      • If b[i] <= a[i], then k == a[i] - b[i].
      • Othrwise, k doesn't exist and the transformation cannot be done.
  • We don't need to actually perform the transformation, we only have to compare the elements of the two arrays (not even all them, in some cases) to find out if such a k value exists. In other words, this algorithm has an O(N) time complexity.

CodePudding user response:

When you are submitting your solution codeforces perform a lot of test cases on it. The error says that it takes too much time so your solution is not optimal for runtime.

After the first look at the code I can give an advise that dynamic allocation is too expensive, you must try preallocation.

  •  Tags:  
  • c
  • Related