Binary Search + PrefixSum | Python | Java
Java Solution\n\nclass Solution {\n    public int waysToSplit(int[] nums) {\n        int ans=0;\n        int MOD = (int) (1e9 + 7);\n        for(int i=1; i<nums
**Java Solution**\n```\nclass Solution {\n    public int waysToSplit(int[] nums) {\n        int ans=0;\n        int MOD = (int) (1e9 + 7);\n        for(int i=1; i<nums.length; i++){\n            nums[i]+=nums[i-1];\n        }\n        for(int i=0; i<nums.length-1; i++){\n            if (nums[i]>nums[nums.length-1]-nums[i])  break;\n            int l=findind(nums, i, nums[i], true);\n            int r=findind(nums, i, nums[i], false);\n            if (l==-1 || r==-1) continue;\n            ans = (ans + (r - l + 1) % MOD) % MOD;\n        }\n        return ans;\n    }\n    \n    public int findind(int[] nums, int ind, int lsum, boolean goleft){\n        int l=ind+1,r=nums.length-1, ans=-1;\n        int N=nums.length;\n        while(l<=r){\n            int m=(l+r)/2;\n            int msum=nums[m]-lsum,rsum=nums[N-1]-nums[m];\n            if(m==N-1) break;\n            if (lsum<=msum && msum<=rsum){\n                ans=m;\n                if (goleft) r=m-1;\n                else l=m+1;\n            }\n            else if (msum>rsum) r=m-1;\n            else  l=m+1;\n        }\n        return ans;\n    }\n}\n```\n**Python Solution**\n```\nclass Solution:\n    def waysToSplit(self, nums: List[int]) -> int:\n        ans=0\n        def find_ind(ind,lsum,goleft):\n            l,r,ans=ind+1,len(nums)-1,-1\n            while l<=r:\n                m=(l+r)//2\n                msum,rsum=nums[m]-lsum,nums[-1]-nums[m]\n                if m==len(nums)-1: break\n                if lsum<=msum<=rsum:\n                    ans=m\n                    if goleft: r=m-1\n                    else: l=m+1\n                elif msum>rsum:\n                    r=m-1\n                else:\n                    l=m+1\n            return ans\n        for i in range(1,len(nums)): nums[i]+=nums[i-1]\n        for i in range(len(nums)-1):\n            if nums[i]>(nums[-1]-nums[i]): break\n            l,r=find_ind(i,nums[i],True),find_ind(i,nums[i],False)\n            if l==-1 or r==-1: continue\n            ans+=r-l+1\n        return ans%1000000007\n```
[]