🚀💻Efficient Solution for Counting Valid Array Splits Using Prefix Sums | JAVA | PYTHON🐍🚀
IntuitionThe problem requires splitting an array into two non-empty parts such that the sum of the left part greater than or 🟰 sum of the right part. To solve t
# Intuition
The problem requires splitting an array into two non-empty parts such that the sum of the left part greater than or 🟰 sum of the right part. To solve this efficiently, we can use a prefix sum 🤷‍♂️ to calculate the sums for both parts in constant time during each iteration.

# Approach
1. Prefix Sum Calculation:
    1. Compute the prefix sum array pref where pref[i] stores the sum of the first i+1 elements.
    2. The sum of the left part for index i is pref[i].
    3. The sum of the right part can be calculated as the total sum of the array minus pref[i].
2. Iterate Through Splits:
   1. For each possible split index i (from 0 to n−2):
   2. Check if the left sum (pref[i]) is greater than or equal to the right sum (total_sum - pref[i]).
   3. If true, increment the count.
3. Return the Count:The count of valid splits is returned as the result.


# Complexity
- Time complexity:
Calculating the prefix sum: O(n).
Iterating through the splits: O(n).
Total: O(n).


- Space complexity:
Prefix sum array pref: O(n).
Total: O(n).


# Code
```java []
class Solution {
    public int waysToSplitArray(int[] nums) {
        long[] pref=new long[nums.length];
        pref[0]=nums[0];
        for(int i=1;i<nums.length;i++){
            pref[i]=pref[i-1]+nums[i];
        }
        int count=0;
        for(int i=0;i<nums.length-1;i++){
            if(pref[i]>=pref[nums.length-1]-pref[i]) count++;
        }
        return count;
    }
}
```
```Python3 []
class Solution:
    def waysToSplitArray(self, nums: List[int]) -> int:
        pref=[nums[0]]
        for i in range(1, len(nums)):
            pref.append(pref[i-1]+nums[i])
        res=0
        for i in range(len(pref)-1):
            if pref[i]>=pref[-1]-pref[i]:
                res+=1
        return res

```




****
['Array', 'Prefix Sum', 'Python', 'Java', 'Python3']