Best Solution for Arrays, Prefix Sum 🚀 in C++, Python and Java || 100% working
Intuition😊 First, calculate the total sum of the array. We will split the array at each index and compare the sum of the left and right parts.Approach👉 Use a pr
# Intuition
😊 First, calculate the total sum of the array. We will split the array at each index and compare the sum of the left and right parts.

# Approach
👉 Use a prefix sum to calculate the left part's sum at each index. Subtract it from the total sum to get the right part's sum.  
✨ Check if the left sum is greater than or equal to the right sum and count such splits.

# Complexity
- Time complexity: $$O(n)$$  
  We traverse the array once to compute the prefix sums and compare them.  

- Space complexity: $$O(1)$$  
  Only a few variables are used.

# Code
```cpp []
#define ll long long
class Solution {
public:
    int waysToSplitArray(vector<int>& nums) {
        ll tot = accumulate(nums.begin(),nums.end(),0LL);
        ll left_sum = 0;
        ll ans = 0;
        for(int i=0;i<nums.size()-1;i++){
            left_sum += nums[i];
            ll right_sum = tot - left_sum;
            if(left_sum >= right_sum){
                ans++;
            }
        }
        return ans;
    }
};
```

```python []
class Solution:
    def waysToSplitArray(self, nums: List[int]) -> int:
        tot = sum(nums)
        left_sum = 0
        ans = 0
        for i in range(len(nums) - 1):
            left_sum += nums[i]
            right_sum = tot - left_sum
            if left_sum >= right_sum:
                ans += 1
        return ans
```

```java []
class Solution {
    public int waysToSplitArray(int[] nums) {
        long tot = 0;
        for (int num : nums) tot += num;
        long left_sum = 0;
        int ans = 0;
        for (int i = 0; i < nums.length - 1; i++) {
            left_sum += nums[i];
            long right_sum = tot - left_sum;
            if (left_sum >= right_sum) {
                ans++;
            }
        }
        return ans;
    }
}
``` 
<img src="https://assets.leetcode.com/users/images/7b864aef-f8a2-4d0b-a376-37cdcc64e38c_1735298989.3319144.jpeg" alt="upvote" width="150px">

# Connect with me on LinkedIn for more insights! 🌟 Link in bio
['Array', 'Prefix Sum', 'Python', 'C++', 'Java', 'Python3']