Kabhi kabhi to lagta hai apunich bhagwan hai
Intuition\nIntuitive\n\n\n\n# Code\n\nclass Solution:\n    def maxOperations(self, nums: List[int]) -> int:\n        # convert to tuple for caching as list are 
# Intuition\nIntuitive\n\n![image.png](https://assets.leetcode.com/users/images/57679fca-c80a-41bb-bb50-bbc922f5574d_1722089044.3101528.png)\n\n# Code\n```\nclass Solution:\n    def maxOperations(self, nums: List[int]) -> int:\n        # convert to tuple for caching as list are not hashable\n        inpnums = tuple(nums)\n\n        @lru_cache\n        def remove(nums, expectedScore):\n            first, last, both = 0, 0, 0\n            if len(nums) >= 2 and expectedScore == nums[0] + nums[1]:\n                first = remove(nums[2:], expectedScore)\n            if len(nums) >= 2 and expectedScore == nums[-2] + nums[-1]:\n                last = remove(nums[:-2], expectedScore)\n            if len(nums) >= 2 and expectedScore == nums[0] + nums[-1]:\n                both = remove(nums[1:-1], expectedScore)\n\n            return max(first, last, both) + 1\n\n        return max(\n            remove(inpnums[1:-1], inpnums[0] + inpnums[-1]),\n            remove(inpnums[:-2], inpnums[-2] + inpnums[-1]),\n            remove(inpnums[2:], inpnums[0] + inpnums[1]),\n        )\n\n```
['Python3']