Python | dp + monotonic stack + two pointers | O(n)
Code\n\nfrom bisect import bisect_right\nclass Solution:\n    def findMaximumLength(self, nums: List[int]) -> int:\n        n = len(nums)\n        stack = [(0,0
# Code\n```\nfrom bisect import bisect_right\nclass Solution:\n    def findMaximumLength(self, nums: List[int]) -> int:\n        n = len(nums)\n        stack = [(0,0,0)]\n        prefix = 0\n        i = 0\n        for j in range(n):\n            prefix += nums[j]\n            i = min(i, len(stack)-1)\n            while i < len(stack) and stack[i][0] <= prefix:\n                i += 1\n            prefixlast, prefi, di = stack[i-1]\n            curprefixlast = prefix + (prefix - prefi)\n            while stack and stack[-1][0] >= curprefixlast:\n                stack.pop()\n            stack.append((curprefixlast, prefix, di+1))\n        return stack[-1][2]\n\n\n\n```
['Python3']