[C++] 3d DP simple
Intuition\nwe can check for 3 different sums i.e. first 2, last 2 ans terminal elements. assign one index for each sum \nlet dp[x][y][ind] = max. operations tha
# Intuition\nwe can check for 3 different sums i.e. first 2, last 2 ans terminal elements. assign one index for each sum \nlet `dp[x][y][ind]` = max. operations that can be done in a subarray for index range `[x, y]` with sum of index `ind`.\n\n`dp[x][y][ind] = 1 + max(dp[x+1][y-1][ind], dp[x+2][y][ind] , dp[x][y-2][ind])`\n\n# Complexity\n- Time complexity:\n$$O(n^2)$$\n\n- Space complexity:\n$$O(3n^2)$$ -->\n\n# Code\n```\nclass Solution {\n    \npublic:\n    int maxOperations(vector<int>& nums) {\n        int n=nums.size();\n        int dp[2001][2001][3];\n        memset(dp, -1, sizeof(dp));\n        map<int, int> indexOf;\n        indexOf[nums[0]+nums[1]] = 0;\n        indexOf[nums[0]+nums[n-1]] = 1;\n        indexOf[nums[n-2]+nums[n-1]] = 2;\n        function<int(int, int, int)> solve =\n         [&](int a, int b, int sum)->int {\n            if(a>=b) return 0;\n            int ans=0;\n            if(dp[a][b][indexOf[sum]]!=-1) return dp[a][b][indexOf[sum]];\n            if(nums[a]+nums[b]==sum)\n                ans = max(ans, 1+ solve(a+1,b-1, sum));\n            if(nums[a]+nums[a+1]==sum)\n                ans = max(ans, 1+ solve(a+2,b, sum));\n            if(nums[b-1]+nums[b]==sum)\n                ans = max(ans, 1+ solve(a,b-2, sum));\n            \n            return dp[a][b][indexOf[sum]] = ans;\n        };\n\n\n        return max({solve(0,n-1,nums[0]+nums[1]), \n                    solve(0,n-1,nums[0]+nums[n-1]),\n                    solve(0,n-1,nums[n-2]+nums[n-1])});\n    }\n};\n```
['C++']