Top-Down | HashMap|Memo
\n\n# Code\n\nimport java.util.HashMap;\n\nclass Solution {\n    public int maxOperations(int[] nums) {\n        return helper(0, nums.length - 1, nums, -1, new
\n\n# Code\n```\nimport java.util.HashMap;\n\nclass Solution {\n    public int maxOperations(int[] nums) {\n        return helper(0, nums.length - 1, nums, -1, new HashMap<>());\n    }\n\n    int helper(int st, int en, int[] nums, int sum, HashMap<String, Integer> memo) {\n        String key = st + "," + en + "," + sum;\n        if (memo.containsKey(key)) {\n            return memo.get(key);\n        }\n        int diff = en - st + 1;\n        if (diff <2) {\n            return 0;\n        }\n        if (sum == -1) {\n            int op1 = 1 + helper(st + 1, en - 1, nums, nums[st] + nums[en], memo);\n            int op2 = 1 + helper(st + 2, en, nums, nums[st] + nums[st + 1], memo);\n            int op3 = 1 + helper(st, en - 2, nums, nums[en] + nums[en - 1], memo);\n            int max = Math.max(op1, Math.max(op2, op3));\n            memo.put(key, max);\n            return max;\n        } else {\n            int val1 = nums[st] + nums[en];\n            int val2 = nums[st] + nums[st + 1];\n            int val3 = nums[en] + nums[en - 1];\n            int op1 = (sum == val1) ? 1 + helper(st + 1, en - 1, nums, sum, memo) : Integer.MIN_VALUE;\n            int op2 = (sum == val2) ? 1 + helper(st + 2, en , nums, sum, memo) : Integer.MIN_VALUE;\n            int op3 = (sum == val3) ? 1 + helper(st, en - 2, nums, sum, memo) : Integer.MIN_VALUE;\n            int max = Math.max(op1, Math.max(op2, op3));\n            memo.put(key, max == Integer.MIN_VALUE ? 0 : max);\n            return max == Integer.MIN_VALUE ? 0 : max;\n        }\n    }\n}\n\n```
['Dynamic Programming', 'Java']