🎉BEATS 100%🏆| O(n)🕒 O(1)🚀 Shortest most easiest optimal code Explained💻 | Running Sum  📈 PHP🌟
CodeIntuition:The function waysToSplitArray calculates the number of ways to split an array of integers nums into two non-empty parts such that the sum of the e

# Code
```php []
class Solution {

    /**
     * @param Integer[] $nums
     * @return Integer
     */
    function waysToSplitArray($nums) {
        $total = array_sum($nums);
        $len = count($nums);
        $count = $leftSum = 0;
        for ($i = 0; $i < $len - 1; $i++) {
            $leftSum += $nums[$i];
            if ($leftSum >= $total - $leftSum) $count++;
        }
        return $count;
    }
}
```


# Intuition:


The function `waysToSplitArray` calculates the number of ways to split an array of integers `nums` into two non-empty parts such that the sum of the elements on the left is greater than or equal to the sum of the elements on the right. Let's break down the intuition:

- **Total Sum Calculation**:  The total sum of the array elements is calculated first using `array_sum($nums)`. This will be used to keep track of the sum of elements on the right side as we iterate through the array.

- **Iterate and Maintain Sums**: We iterate through the array from the first element to the second-to-last element. For each iteration, we calculate the running sum of elements from the left (`$leftSum`), and compare it with the sum of elements from the right (`$total - $leftSum`).

- **Count Valid Splits**: Each time the condition `if ($leftSum >= $total - $leftSum)` is satisfied, it means that splitting the array at this point (after the current element) results in a valid partition where the left part has a sum greater than or equal to the right part. We increment the counter `$count` each time this condition is met.

- **Return the Result**: Finally, the function returns the value of `count`, which represents the number of valid ways to split the array.


--- 
![image.png](https://assets.leetcode.com/users/images/43870a4a-7b2b-42c8-b921-4fa8380bc3db_1735901655.6578972.png)

# Approach:

1. **Initialization**: 
   - First, we calculate the total sum of all elements in the array. This will help us calculate the sum of the right part of the array dynamically as we update the left sum.
   - We also initialize a variable `$leftSum` to track the sum of elements from the left part, and a variable `$count` to keep track of the number of valid splits.

2. **Loop through the Array**: 
   - We loop through the array from index 0 to `len - 2` (since we need at least one element on both sides of the split).
   - In each iteration, we add the current element to the left sum (`leftSum`), and then check if the left sum is greater than or equal to the right sum (`total - leftSum`).
   - If the condition is satisfied, we increment the count of valid splits.

-----
# Complexity:

### Time Complexity: **O(n)**

- **Calculating the Total Sum**: `array_sum($nums)` takes **O(n)** time, where `n` is the length of the array.
- **Iterating Over the Array**: The `for` loop iterates through `n-1` elements, and for each iteration, constant-time operations (addition, subtraction, comparison) are performed. Therefore, the loop runs in **O(n)** time.

Thus, the **total time complexity** is **O(n)**, where `n` is the number of elements in the array.

### Space Complexity: **O(1)**

- The space complexity is **O(1)** because the solution only uses a constant amount of extra space: `total`, `leftSum`, and `count` (all variables are scalar values).


['Array', 'PHP']