Prefix Sum with O(1) space||C++beats 100%
IntuitionFind the number of ways splitting the array nums such that sum(nums[0...i])>=sum(nums[i+1...]).
How?
compute sum=sum(nums)
Use a loop to find whether i
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Find the number of ways splitting the array `nums` such that `sum(nums[0...i])>=sum(nums[i+1...])`.
How?
1. compute `sum=sum(nums)`
2. Use a loop to find whether i satisfying `sum(nums[0..i])>=sum(nums[i+1..])=sum-sum(nums[0..i])`

2 pass solution. Python is also made
Using inequality $$\sum_{k=0}^i nums[k]\geq \sum_{k=i+1 }^{n-1}nums[k] \iff 2 \sum_{k=0}^i nums[k]\geq sum$$ can simplify the code.
# Approach
<!-- Describe your approach to solving the problem. -->
[Please  Turn on English subtitles if necessary]
[https://youtu.be/WQLBMjpeal4?si=_-2AouMRypgCpRGi](https://youtu.be/WQLBMjpeal4?si=_-2AouMRypgCpRGi)
1. Compute `sum=sum(nums)` by using `std::accumulate`
2. Set `acc=0` as the variable for prefix sum (Note using long long to avoid of overflowing)
3. Initialize `cnt=0`
4. Procced a loop as follow
```
        for(int i=0; i<n-1; i++){
            acc+=nums[i];// prefix sum
            cnt+=(2*acc>=sum);// acc>=sum/2.0<=>acc>=sum-acc
        }

```
5. `cnt` is the answer
6. Python code is made with the same logic.
7. A slight variant of C++ is made by using `std::count_if` with lambda instead of the for-loop in the 1st C++.
8. 2nd Python using accumulate(  for Python prefix sum). A 2-line code is made. In contrary to Python, C++ `std::partial_sum` may have problem with int overflowing for using the similar method.
9. Python 1-liner is done modified from the 2-line code. More 1-liner codes can be ref the comments by @maria_q ref. https://leetcode.com/problems/number-of-ways-to-split-array/solutions/6223222/one-line-solution/
# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O( n)$$-->
$O(n)$
- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
$O(1)$
# Code||C++ 0ms beats 100%
```cpp []
class Solution {
public:
    static int waysToSplitArray(vector<int>& nums) {
        const int n=nums.size();
        long long sum=accumulate(nums.begin(), nums.end(), 0LL);
        long long acc=0LL;
        int cnt=0;
        for(int i=0; i<n-1; i++){
            acc+=nums[i];
            cnt+=(2*acc>=sum);
        }
        return cnt;
    }
};
```
```Python []
class Solution:
    def waysToSplitArray(self, nums: List[int]) -> int:
        n=len(nums)
        Sum=sum(nums)
        acc, cnt=0, 0
        for i in range(n-1):
            acc+=nums[i]
            cnt+=(2*acc>=Sum)
        return cnt
        
        
```
# 2nd C++ using count_if instead of the for-loop
```
class Solution {
public:
    static int waysToSplitArray(vector<int>& nums) {
        long long sum=accumulate(nums.begin(), nums.end(), 0LL);
        return count_if(nums.begin(), nums.end()-1, [acc=0LL, sum](int x) mutable{
            acc+=x;
            return 2*acc>=sum;
        });
    }
};
```
# 2nd Python 2-line TC: O(n) SC: O(n)
```
class Solution:
    def waysToSplitArray(self, nums: List[int]) -> int:
        nums=list(accumulate(nums))
        return sum(1 if 2*x>=nums[-1] else 0 for x in nums[0:len(nums)-1])
```
# Python 1-liner 
```
class Solution:
    def waysToSplitArray(self, nums: List[int]) -> int:
        return (nums:=list(accumulate(nums))) and sum(2*x>=nums[-1] for x in nums[0:-1])
```
['Math', 'Prefix Sum', 'C++', 'Python3']