[C++] Recursion + Memoization || Easy to understand
Recursion (TLE)\n\nclass Solution {\npublic:\n    int maxOperations(vector<int>& nums) {\n        if(nums.size() == 2) return 1;\n        int n = nums.size();\n
# Recursion (TLE)\n```\nclass Solution {\npublic:\n    int maxOperations(vector<int>& nums) {\n        if(nums.size() == 2) return 1;\n        int n = nums.size();\n        int ans = solve(nums , n , nums[0]+nums[1]);\n        ans = max(ans , solve(nums , n , nums[0]+nums[n-1]));\n        ans = max(ans , solve(nums , n , nums[n-1]+nums[n-2]));\n        return ans;\n    }\n    int solve(vector<int>& nums , int n , int target){\n        int ans = helper(nums , 0 , n-1 , target);\n        return ans;\n    }\n    int helper(vector<int>& nums , int l , int r , int target){\n        if(l >= r) return 0;\n        int ans = 0;\n        if(nums[l]+nums[r] == target){\n            ans = max(ans , 1 + helper(nums , l+1 , r-1 , target));\n        }\n        if(nums[l]+nums[l+1] == target){\n            ans = max(ans , 1 + helper(nums , l+2 , r , target));\n        }\n        if(nums[r]+nums[r-1] == target){\n            ans = max(ans , 1 + helper(nums , l , r-2 , target));\n        }\n        return ans;\n    }\n};\n```\n\n# Recursion + Memoization\n```\nclass Solution {\npublic:\n    int maxOperations(vector<int>& nums) {\n        if(nums.size() == 2) return 1;\n        int n = nums.size();\n        int ans = solve(nums , n , nums[0]+nums[1]);\n        ans = max(ans , solve(nums , n , nums[0]+nums[n-1]));\n        ans = max(ans , solve(nums , n , nums[n-1]+nums[n-2]));\n        return ans;\n    }\n    int solve(vector<int>& nums , int n , int target){\n        vector<vector<int>> dp(n , vector<int>(n , -1));\n        int ans = helper(nums , 0 , n-1 , target , dp);\n        return ans;\n    }\n    int helper(vector<int>& nums , int l , int r , int target , vector<vector<int>>& dp){\n        if(l >= r) return 0;\n        if(dp[l][r] != -1) return dp[l][r];\n        int ans = 0;\n        if(nums[l]+nums[r] == target){\n            ans = max(ans , 1 + helper(nums , l+1 , r-1 , target , dp));\n        }\n        if(nums[l]+nums[l+1] == target){\n            ans = max(ans , 1 + helper(nums , l+2 , r , target , dp));\n        }\n        if(nums[r]+nums[r-1] == target){\n            ans = max(ans , 1 + helper(nums , l , r-2 , target , dp));\n        }\n        return dp[l][r] = ans;\n    }\n};\n```\n# Please upvote If you find this helpfull
['Dynamic Programming', 'Recursion', 'Memoization', 'C++']