Video Explanation (Optimising brute force solution step by step)
Explanation\n\nClick here for the video\n\n# Code\n\nint dp[2001][2001];\n\nclass Solution {\n    \n    vector<int> arr;\n    \n    int maxOperations (int l, in
# Explanation\n\n[Click here for the video](https://youtu.be/9iYY33fuLSo)\n\n# Code\n```\nint dp[2001][2001];\n\nclass Solution {\n    \n    vector<int> arr;\n    \n    int maxOperations (int l, int r, const int target) {\n        if (l >= r) return 0;\n        \n        int &ans = dp[l][r];\n        if (ans != -1) return ans;\n        \n        ans = 0;\n        if (arr[l]+arr[r] == target) ans = max (ans, 1+maxOperations(l+1, r-1, target));\n        if (arr[l]+arr[l+1] == target) ans = max (ans, 1+maxOperations(l+2, r, target));\n        if (arr[r]+arr[r-1] == target) ans = max (ans, 1+maxOperations(l, r-2, target));\n        \n        return ans;\n    }\n    \n    int MaxOperationsWithSum (int sum) {\n        memset(dp, -1, sizeof(dp));\n        return maxOperations (0, arr.size()-1, sum);\n    }\n    \npublic:\n    int maxOperations(vector<int>& nums) {\n        arr = nums;\n        int n = arr.size();\n        \n        if (n == 1) return 0;\n        \n        return max ({\n            MaxOperationsWithSum(arr[0] + arr[1]),\n            MaxOperationsWithSum(arr[n-1] + arr[n-2]),\n            MaxOperationsWithSum(arr[0] + arr[n-1])\n        });\n    }\n};\n```
['C++']