Java Solution with Pair
\nimport java.util.Arrays;\n\nclass Solution {\n    public int findMaximumLength(int[] nums) {\n        int n = nums.length;\n\n        long[] pre = new long[n 
```\nimport java.util.Arrays;\n\nclass Solution {\n    public int findMaximumLength(int[] nums) {\n        int n = nums.length;\n\n        long[] pre = new long[n + 1];\n        for (int i = 1; i <= n; i++)\n            pre[i] = pre[i - 1] + nums[i - 1];\n\n        Pair[] dp = new Pair[n + 1];\n        for (int i = 0; i <= n; i++)\n            dp[i] = new Pair(0, Long.MAX_VALUE);\n\n        dp[0] = new Pair(0, 0);\n        for (int i = 1; i <= n; i++) {\n            long prevSum = dp[i - 1].second;\n            long needed = pre[i - 1] + prevSum;\n\n            int low = i;\n            int high = n;\n            int where = -1;\n            while (low <= high) {\n                int mid = (low + high) / 2;\n\n                if (pre[mid] >= needed) {\n                    where = mid;\n                    high = mid - 1;\n                } else\n                    low = mid + 1;\n            }\n            if (where != -1) {\n                Pair p = new Pair(dp[i - 1].first + 1, pre[where] - pre[i - 1]);\n\n                if (dp[where].first < dp[i - 1].first + 1)\n                    dp[where] = p;\n                else if (dp[where].first == dp[i - 1].first + 1)\n                    dp[where].second = Math.min(dp[where].second, pre[where] - pre[i - 1]);\n            }\n\n            if (dp[i].first < dp[i - 1].first)\n                dp[i] = new Pair(dp[i - 1].first, dp[i - 1].second + pre[i] - pre[i - 1]);\n            else if (dp[i].first == dp[i - 1].first)\n                dp[i].second = Math.min(dp[i].second, pre[i] - pre[i - 1] + dp[i - 1].second);\n        }\n\n        int ans = 0;\n        for (Pair pair : dp)\n            ans = Math.max(ans, pair.first);\n\n        return ans;\n    }\n\n    // Custom Pair class to hold two values\n    class Pair {\n        int first;\n        long second;\n\n        public Pair(int first, long second) {\n            this.first = first;\n            this.second = second;\n        }\n    }\n}\n\n```
[]