C++ | Prefix Sum + Binary Search
\n# Code\n\nclass Solution {\npublic:\n    int mod = 1e9+7;\n    int waysToSplit(vector<int>& nums) {\n        int n = nums.size();\n        vector<int> pref(n)
\n# Code\n```\nclass Solution {\npublic:\n    int mod = 1e9+7;\n    int waysToSplit(vector<int>& nums) {\n        int n = nums.size();\n        vector<int> pref(n);\n        pref[0] = nums[0];\n        for(int i=1;i<n;i++)\n        {\n            pref[i] = pref[i-1]+nums[i];\n        }\n        int ans=0;\n        for(int i=n-1;i>=2;i--)\n        {\n            int l = 1,r = i-1;\n            int sum3 = pref[n-1]-pref[i-1];\n            int idx1 = -1,idx2 = -1;\n            while(l<=r)\n            {\n                int mid = (l+r)/2;\n                int sum1 = pref[mid-1];\n                int sum2 = pref[i-1]-sum1;\n                if(sum1<=sum2 && sum2<=sum3 && sum1<=sum3)\n                {\n                    idx1 = mid;\n                    l = mid+1;\n                }\n                else if(sum1>sum2)\n                r = mid-1;\n                else\n                l = mid+1;\n            }\n            l = 1,r= i-1;\n            while(l<=r)\n            {\n                int mid = (l+r)/2;\n                int sum1 = pref[mid-1];\n                int sum2 = pref[i-1]-sum1;\n                if(sum1<=sum2 && sum2<=sum3 && sum1<=sum3)\n                {\n                    idx2 = mid;\n                    r = mid-1;\n                }\n                else if(sum1>sum2)\n                r = mid-1;\n                else \n                l = mid+1;\n            }\n            if(idx1!=idx2 && idx1!=-1 && idx2!=-1)\n            {\n                ans = (ans+((idx1-idx2)+1))%mod;\n            }\n            else if(idx1!=-1 || idx2!=-1)\n            {\n                ans = (ans+1)%mod;\n            }\n        }\n        return ans;\n    }\n};\n\n```
['C++']