Concise JAVA Recursion
\n\n# Approach\nwe can see it\'s a DP problem just by noticing we have to try all the approaches. We can fix the initital sum to reduce one extra value to memoi
\n\n# Approach\nwe can see it\'s a DP problem just by noticing we have to try all the approaches. We can fix the initital sum to reduce one extra value to memoize.\n\n# Complexity\n- Time complexity:\n   Roughly O(N) where N is the lenth of the array nums \n\n- Space complexity:\n  Initial 2D dp array O(N*N) {Ignoring the stack space}\n\n# Code\n```\nclass Solution {\n    public int maxOperations(int[] nums) {\n        int n = nums.length;\n        int dp[][] = new int[n][n];\n        if(n < 2)\n            return 0;\n        if(n == 2)\n            return 1;\n        for(int i = 0 ; i < n ; i++) {\n            Arrays.fill(dp[i], 0);\n        }\n        int max = 0;\n        \n        max = Math.max(max, foo(nums, nums[0] + nums[1], 2, n - 1, dp));\n        for(int i = 0 ; i < n ; i++) {\n            Arrays.fill(dp[i], 0);\n        }\n        max = Math.max(max, foo(nums, nums[0] + nums[n-1], 1, n - 2, dp));\n        for(int i = 0 ; i < n ; i++) {\n            Arrays.fill(dp[i], 0);\n        }\n        max = Math.max(max, foo(nums, nums[n-1] + nums[n-2], 0, n-3, dp));\n        return max + 1;\n    }\n    int foo(int nums[], int prev, int l, int r,int dp[][]) {\n        if(l >= r) {\n            return 0;\n        }\n        if(dp[l][r] != 0) {\n            return dp[l][r];\n        }\n        if(l + 1 <= r && nums[l] + nums[l+1] == prev) {\n            dp[l][r] = Math.max(dp[l][r],  1 + foo(nums, prev, l + 2, r, dp));\n        }\n        if(nums[l] + nums[r] == prev) {\n            dp[l][r] = Math.max(dp[l][r], 1 + foo(nums, prev, l + 1, r - 1, dp));\n        }\n        if(r - 1 >= l && nums[r] + nums[r-1] == prev) {\n            dp[l][r] = Math.max(dp[l][r], 1 + foo(nums, prev, l, r - 2, dp));\n        }\n        return dp[l][r];\n    }\n}\n```
['Dynamic Programming', 'Recursion', 'Memoization', 'Java']