Beats 100% | Prefix Sum | Solution for LeetCode#2270
IntuitionThe problem asks us to split an array into two non-empty parts such that the sum of the left part is greater than or equal to the sum of the right part
![image.png](https://assets.leetcode.com/users/images/bd234d24-e798-43a3-9c06-798b722924f3_1735866361.2195225.png)

# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
The problem asks us to split an array into two non-empty parts such that the sum of the left part is greater than or equal to the sum of the right part. To solve this efficiently, we can calculate the total sum of the array once and then iterate through the array while maintaining a running sum for the left part.

# Approach
<!-- Describe your approach to solving the problem. -->
1. Calculate Total Sum:
	- Compute the total sum of the array (sumTotal) in a single pass.
2. Iterate Through the Array:
	- Maintain a running sum (sumCurr) for the left part of the array as you iterate.
	- For each index i (except the last one, since both parts must be non-empty), check if sumCurr >= sumTotal - sumCurr. If true, increment a counter (cnt).
3. Return Result:
	- After iterating through the array, return cnt, which represents the number of valid splits.
	
# Complexity
- Time complexity: O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code Option-01
```java []
class Solution {
    public int waysToSplitArray(int[] nums) {
        int n=nums.length;
        long sumTotal=0;
        long sumCurr=0;
        int cnt=0;

        // Calculate total sum
        for(int i=0; i<n; i++){
            sumTotal+=nums[i];
        }

        // Check valid splits condition
        for(int i=0; i<n-1; i++){
            sumCurr+=nums[i];
            if(sumCurr >= sumTotal-sumCurr){
                cnt++;
            }
        }

        return cnt;
    }
}
```

# Code Option-02 : Used Prefix Sum
![image.png](https://assets.leetcode.com/users/images/2503ae43-bf06-4d09-bfee-99ccfc8d515c_1735866626.0509033.png)

- Time complexity: O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: O(n)

```java []
class Solution {
    public int waysToSplitArray(int[] nums) {
        int n=nums.length;
        int m=n+1;
        long[] arr=new long[m];
        int cnt=0;

        //Doing the Prefix sum
        arr[0]=0;
        for(int i=1; i<m; i++){
            arr[i]=arr[i-1]+nums[i-1];
        }

        //Checking a valid split 
        for(int i=1; i<m-1; i++){
            if(arr[i] >= arr[m-1]-arr[i]){
                cnt++;
            }
        }

        return cnt;
    }
}
```
['Prefix Sum', 'Java']