[C++] DP [Recursion + Memoization] Easy and Straight Forward
\nclass Solution {\npublic:\n    int solve(vector<vector<int>> &dp, vector<int> &nums, int l, int r, int val, int taken)\n    {\n        int n = nums.size(), an
```\nclass Solution {\npublic:\n    int solve(vector<vector<int>> &dp, vector<int> &nums, int l, int r, int val, int taken)\n    {\n        int n = nums.size(), ans = taken;\n        if(l > n || r < 0 || l >= r)return taken;\n        if(dp[l][r]!=-1)return dp[l][r];\n        if(l+1 < n && nums[l+1] + nums[l] == val)ans = max(ans, solve(dp, nums, l+2, r, val, 1 + taken));\n        if(nums[r] + nums[l] == val)ans = max(ans, solve(dp, nums, l+1, r-1, val, taken + 1));\n        if(r-1 >= 0 && nums[r-1] + nums[r] == val)ans = max(ans, solve(dp, nums, l, r-2, val, taken + 1));\n        return dp[l][r] = ans;\n    }\n    int maxOperations(vector<int>& nums) {\n        int n = nums.size();\n        vector<vector<int>> dp1(n+1, vector<int> (n+1, -1));\n        vector<vector<int>> dp2(n+1, vector<int> (n+1, -1));\n        vector<vector<int>> dp3(n+1, vector<int> (n+1, -1));\n        return max({solve(dp1, nums, 0, n-1, nums[0]+nums[1], 0), \n                    solve(dp2, nums, 0, n-1, nums[0]+nums[n-1], 0),solve(dp3, nums, 0, n-1, nums[n-1]+nums[n-2], 0)});\n    }\n};\n```
['Dynamic Programming', 'C']