Simple Java | DP | Memoization | O(n^2) | Beats 98.77%
Approach\nTry All possible paths and maintain a 2-D matrix to store the results.\n\n# Complexity\n- Time complexity: O(n^2)\n\n- Space complexity: O(n^2)\n\n# C
# Approach\nTry All possible paths and maintain a 2-D matrix to store the results.\n\n# Complexity\n- Time complexity: O(n^2)\n\n- Space complexity: O(n^2)\n\n# Code\n```\nclass Solution {\n    public int maxOperations(int[] nums) {\n        int [][] map = new int[nums.length][nums.length];\n        int start = getMax(nums, 2, nums.length - 1, nums[0] + nums[1], map) + 1;\n        int end = getMax(nums, 0, nums.length - 3, nums[nums.length - 1] + nums[nums.length - 2], map) + 1;\n        int both = getMax(nums, 1, nums.length - 2, nums[0] + nums[nums.length - 1], map) + 1;\n        return Math.max(start, Math.max(end, both));\n    }\n    private int getMax(int nums[], int start, int end, int pastVal, int[][] map) {\n        if (start >= end) {\n            return 0;\n        }\n        if (map[start][end] != 0) {\n            return map[start][end];\n        }\n        int max = 0;\n        int startVal = nums[start] + nums[start + 1];\n        if (startVal == pastVal) {\n            max = Math.max(max, getMax(nums, start + 2, end, pastVal, map) + 1);\n        }\n        int endVal = nums[end] + nums[end - 1];\n        if (endVal == pastVal) {\n            max = Math.max(max, getMax(nums, start, end - 2, pastVal, map) + 1);\n        }\n        int both = nums[start] + nums[end];\n        if (both == pastVal) {\n            max = Math.max(max, getMax(nums, start + 1, end - 1, pastVal, map) + 1);\n        }\n        map[start][end] = max;\n        return max;\n    }\n}\n```
['Java']