One of the simplest solution
\n\nclass Solution {\n    Integer[][][]dp;\n    int[]k;\n    public int maxOperations(int[] nums) {\n        dp=new Integer[nums.length][nums.length][4];\n     
\n```\nclass Solution {\n    Integer[][][]dp;\n    int[]k;\n    public int maxOperations(int[] nums) {\n        dp=new Integer[nums.length][nums.length][4];\n        k=new int[4];\n        k[1]=nums[nums.length-1]+nums[nums.length-2];\n        k[2]=nums[0]+nums[nums.length-1];\n        k[3]=nums[0]+nums[1];\n        return f(0,nums.length-1,nums,0);\n    }\n    public int f(int l,int r,int[]nums,int s){\n        if(l>=r)return 0;\n         int ans=0;\n        if(dp[l][r][s]!=null)return dp[l][r][s];\n\n       \n          // System.out.println(s+" "+ans+" "+k[s]+" "+(nums[l]+nums[r])+" "+(nums[l]+nums[l+1])+" "+(nums[r]+nums[r-1])+" "+l+" "+r);\n            if(s==0){\n                ans=Math.max(ans,1+f(l,r-2,nums,1));\n              ans=Math.max(1+f(l+1,r-1,nums,2),ans);\n              ans=Math.max(ans,1+f(l+2,r,nums,3));\n              \n            }\n            else{\n                if(k[s]==nums[l]+nums[r]){\n                  ans=Math.max(1+f(l+1,r-1,nums,s),ans);\n                }\n                if(nums[l]+nums[l+1]==k[s]){\n                    ans=Math.max(ans,1+f(l+2,r,nums,s));\n                }\n                if(nums[r]+nums[r-1]==k[s]){\n                      ans=Math.max(ans,1+f(l,r-2,nums,s));\n                }\n            }\n       // System.out.println(s+" "+ans+" "+" "+(nums[l]+nums[r])+" "+(nums[l]+nums[l+1])+" "+(nums[r]+nums[r-1])+" "+l+" "+r);\n            return dp[l][r][s]=ans;\n    }\n}\n```
['Java']