🔥BEATS 💯 % 🎯 |✨SUPER EASY BEGINNERS 👏
IntuitionTo 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, we can leverage pr
![image.png](https://assets.leetcode.com/users/images/d10e4384-7aa2-4c00-909d-f5a79cdcf3aa_1735907585.4101338.png)


---

### Intuition
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, we can leverage prefix and suffix sums. We maintain a running total for the left and right sums as we iterate through the array.

---

### Approach
1. Calculate the total sum of the array, which will initially represent the right sum.
2. Iterate through the array up to the second-to-last element.
   - Update the left sum by adding the current element.
   - Update the right sum by subtracting the current element.
   - Check if the left sum is greater than or equal to the right sum.
3. Increment the result counter if the condition is satisfied.
4. Return the final result.

---

### Complexity
- **Time Complexity**: $$O(n)$$, where $$n$$ is the length of the array, as we traverse the array once.
- **Space Complexity**: $$O(1)$$, as we use constant extra space.

---


```dart []
class Solution {
  int waysToSplitArray(List<int> nums) {
    int res = 0;
    int leftSum = 0;
    int rightSum = nums.reduce((a, b) => a + b); // Total sum
    int n = nums.length;

    for (int i = 0; i < n - 1; i++) {
      leftSum += nums[i];
      rightSum -= nums[i];
      if (leftSum >= rightSum) res++;
    }

    return res;
  }
}
```


```python []
class Solution:
    def waysToSplitArray(self, nums: List[int]) -> int:
        res = 0
        left_sum = 0
        right_sum = sum(nums)  # Total sum

        for i in range(len(nums) - 1):
            left_sum += nums[i]
            right_sum -= nums[i]
            if left_sum >= right_sum:
                res += 1

        return res
```


```java []
class Solution {
    public int waysToSplitArray(int[] nums) {
        long leftSum = 0, rightSum = 0;
        int res = 0;

        // Calculate total sum
        for (int num : nums) {
            rightSum += num;
        }

        // Iterate through the array
        for (int i = 0; i < nums.length - 1; i++) {
            leftSum += nums[i];
            rightSum -= nums[i];
            if (leftSum >= rightSum) {
                res++;
            }
        }

        return res;
    }
}
```


```cpp []
class Solution {
public:
    int waysToSplitArray(vector<int>& nums) {
        long long leftSum = 0, rightSum = accumulate(nums.begin(), nums.end(), 0LL);
        int res = 0;

        for (int i = 0; i < nums.size() - 1; i++) {
            leftSum += nums[i];
            rightSum -= nums[i];
            if (leftSum >= rightSum) {
                res++;
            }
        }

        return res;
    }
};
```


```javascript []
var waysToSplitArray = function(nums) {
    let leftSum = 0;
    let rightSum = nums.reduce((a, b) => a + b, 0); // Total sum
    let res = 0;

    for (let i = 0; i < nums.length - 1; i++) {
        leftSum += nums[i];
        rightSum -= nums[i];
        if (leftSum >= rightSum) {
            res++;
        }
    }

    return res;
};
```


```go []
func waysToSplitArray(nums []int) int {
    leftSum, rightSum := 0, 0
    for _, num := range nums {
        rightSum += num
    }

    res := 0
    for i := 0; i < len(nums)-1; i++ {
        leftSum += nums[i]
        rightSum -= nums[i]
        if leftSum >= rightSum {
            res++
        }
    }

    return res
}
```

---
![image.png](https://assets.leetcode.com/users/images/d81a7cee-fa73-41a2-82dc-65bf475d960c_1722267693.3902693.png) {:style='width:250px'}


['Array', 'C', 'Prefix Sum', 'C++', 'Java', 'Go', 'Python3', 'JavaScript', 'Dart']