Need help with memoization part DP.
Question Link :-
https://leetcode.com/problems/longest-arithmetic-subsequence-of-given-difference/

Below is a recursive solution to the problem (Passes 33/39 test cases , later gives TLE).
Would be great if someone could help to memoize this.

class Solution {
public:
    int maxi = INT_MIN;
    int max(int a,int b){
        return (a>b)?a:b;
    }
    int helper(vector<int>&arr,int diff,vector<int> x,int i){
        if(i == arr.size()){
            maxi = max(maxi,x.size());
            return maxi;
        }
        int a = 0,b=0,c=0,d=0;
        if(i == 0 or x.size() == 0){
            x.push_back(arr[i]);
            a = helper(arr,diff,x,i+1);
            x.pop_back();
            b = helper(arr,diff,x,i+1);
            return max(a,b);
        }
        else if(x.size() != 0){
              if(arr[i] - x[x.size()-1] == diff){
                    x.push_back(arr[i]);
                    return c=helper(arr,diff,x,i+1);
                }
            else{
                    return d=helper(arr,diff,x,i+1);
                }
        }
        return 1;
    }
    int longestSubsequence(vector<int>& arr, int diff) {
        vector<int> x;
        
        helper(arr,diff,x,0);
        
        return maxi;
    }
};
Comments (2)