Getting run error for few test cases

I am getting run error for the code below. Also getting TLE on Leetcode with the approach used in the solution video give. Give some suggestions to get rid of TLE.

try to dry run your code for input
[23, 2, 4, 6, 7]
you’ll get your error
also please share the code you are submitting on leetcode, you are only supposed to fill the function there

you can try this to solve the tle:

    int ans=0;
    for(int i=0;i<n;i++){
        
        for(int j=0;j<i;j++){
            int diff=A[i]-A[j]+500;
            int &x=dp[i][diff],&y=dp[j][diff];
            x=max(x,1+y);
            ans=max(ans,1+x);
        }
    }
    return ans;

dp state is simple

The error is due to spaces between the integers in the list. It is due to wrong inputs.
Removed the spaces and it is working fine. And yes, I am writing the same function only on Leetcode as given in the link. Not sure why this is leading to TLE.

Thanks Abha :slight_smile: