DP + Memory
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```csharp []\npublic class Solution {\n    private int[,] dp;\n   \n    private int MaxOpts(int[] nums, int left, int right, int preSum)\n    {\n        if(left >= right)\n            return 0;\n\n        if(dp[left, right] != 0)\n            return dp[left, right];\n        \n         int maxCnt = 0;\n        if(nums[left] + nums[left+1] == preSum)\n        {\n            maxCnt = Math.Max(maxCnt, 1+ MaxOpts(nums, left+2, right, preSum));\n        }\n\n        if(nums[left] + nums[right] == preSum)\n        {\n            maxCnt = Math.Max(maxCnt, 1+ MaxOpts(nums, left+1, right-1, preSum));\n        }\n\n        if(nums[right-1] + nums[right] == preSum)\n        {\n            maxCnt = Math.Max(maxCnt, 1+ MaxOpts(nums, left, right-2, preSum));\n        }\n\n        dp[left, right] = maxCnt;\n        return maxCnt;\n    }\n    public int MaxOperations(int[] nums) {\n        int len = nums.Length;\n        if(len < 2)\n            return 0;\n\n        HashSet<int> items = new HashSet<int>(nums);\n        if(items.Count == 1)\n            return len/2;\n        dp = new int[len, len];\n       \n        int res = 1;\n        res = Math.Max(res, 1+MaxOpts(nums, 1, len-2, nums[0] + nums[len-1])); \n        res = Math.Max(res, 1+MaxOpts(nums, 2, len-1, nums[0] + nums[1]));\n        res = Math.Max(res, 1+MaxOpts(nums, 0, len-3, nums[len-2] + nums[len-1]));\n\n        return res;\n    }\n}\n```
['C#']