> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Number of Ways to Split Array Python Solution

> Tested Python solution for LeetCode 2270 with 20 pytest cases. Generate a practice environment with lcpy.

LeetCode 2270, [Medium](/catalog/medium). Topics: [Array](/catalog/topics/array), [Prefix Sum](/catalog/topics/prefix-sum). [View on LeetCode](https://leetcode.com/problems/number-of-ways-to-split-array/description/).

Generate this problem as a practice environment: tested reference solution, 20 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2270   # by problem number
lcpy gen -s number_of_ways_to_split_array   # by problem name
```

## Problem

You are given a \<strong>0-indexed\</strong> integer array \<code>nums\</code> of length \<code>n\</code>.

\<p>\<code>nums\</code> contains a \<strong>valid split\</strong> at index \<code>i\</code> if the following are true:\</p>

\<ul>
\<li>The sum of the first \<code>i + 1\</code> elements is \<strong>greater than or equal to\</strong> the sum of the last \<code>n - i - 1\</code> elements.\</li>
\<li>There is \<strong>at least one\</strong> element to the right of \<code>i\</code>. That is, \<code>0 \<= i \< n - 1\</code>.\</li>
\</ul>

\<p>Return \<em>the number of \<strong>valid splits\</strong> in\</em> \<code>nums\</code>.\</p>

### Examples

```
Input: nums = [10,4,-8,7]
Output: 2
Explanation:
There are three ways of splitting nums into two non-empty parts:
- Split nums at index 0. Then, the first part is [10], and its sum is 10. The second part is [4,-8,7], and its sum is 3. Since 10 >= 3, i = 0 is a valid split.
- Split nums at index 1. Then, the first part is [10,4], and its sum is 14. The second part is [-8,7], and its sum is -1. Since 14 >= -1, i = 1 is a valid split.
- Split nums at index 2. Then, the first part is [10,4,-8], and its sum is 6. The second part is [7], and its sum is 7. Since 6 < 7, i = 2 is not a valid split.
Thus, the number of valid splits in nums is 2.
```

```
Input: nums = [2,3,1,0]
Output: 2
Explanation:
There are two valid splits in nums:
- Split nums at index 1. Then, the first part is [2,3], and its sum is 5. The second part is [1,0], and its sum is 1. Since 5 >= 1, i = 1 is a valid split.
- Split nums at index 2. Then, the first part is [2,3,1], and its sum is 6. The second part is [0], and its sum is 0. Since 6 >= 0, i = 2 is a valid split.
```

### Constraints

* 2 \<= nums.length \<= 10^5
* -10^5 \<= nums\[i] \<= 10^5

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_ways_to_split_array/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/number_of_ways_to_split_array/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(n)
    # Space: O(1)
    def ways_to_split_array(self, nums: list[int]) -> int:
        total = sum(nums)
        left = 0
        valid = 0
        for i in range(len(nums) - 1):
            left += nums[i]
            if left >= total - left:
                valid += 1
        return valid
```

## Complexity

| Time | Space |
| - | - |
| O(n) | O(1) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.