Easy to read dp solution - 277 ms
Approach\nWe start from 3 possible cases (3 sums) and later we just find maximum using memorization. In the final result 1 + means two first sum numbers as they
# Approach\nWe start from 3 possible cases (3 sums) and later we just find maximum using memorization. In the final result `1 +` means two first sum numbers as they not counted in `find` function. Memorization is required, because it will fail even on small test like:\n`[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]`\n\n# Complexity\n- Time complexity:\n$$O(n ^ 2)$$\n\n- Space complexity:\n$$O(n ^ 2)$$\n\n# Code\n```\n/**\n * @param {number[]} nums\n * @return {number}\n */\nvar maxOperations = function(nums) {\n    const dp = Array.from({ length: nums.length }, () => new Array(nums.length));\n\n    const find = (l, r, sum) => {\n        if (r - l < 1) return 0;\n\n        if (dp[l][r] !== undefined) return dp[l][r];\n\n        let ret = 0;\n\n        if (nums[l] + nums[l + 1] === sum) ret = Math.max(ret, 1 + find(l + 2, r, sum));\n        if (nums[l] + nums[r] === sum) ret = Math.max(ret, 1 + find(l + 1, r - 1, sum));\n        if (nums[r - 1] + nums[r] === sum) ret = Math.max(ret, 1 + find(l, r - 2, sum));\n\n        return dp[l][r] = ret;\n    };\n\n    return 1 + Math.max(\n        find(2, nums.length - 1, nums[0] + nums[1]),\n        find(1, nums.length - 2, nums[0] + nums[nums.length - 1]),\n        find(0, nums.length - 3, nums[nums.length - 2] + nums[nums.length - 1])\n    );\n};\n```
['JavaScript']