Step-by-Step Solution: Number of Ways to Split Array (100% Easy, 0ms)
IntuitionThe problem is about finding the number of ways to split an array into two non-empty parts such that the sum of the first part is greater than or equal
# Intuition
The problem is about finding the number of ways to split an array into two non-empty parts such that the sum of the first part is greater than or equal to the sum of the second part. The **prefix sum technique** provides an efficient way to calculate the cumulative sum and perform quick range queries.

# Approach
1. **Prefix Sum Calculation**:  
   - Compute the cumulative sum for the array using a `prefixSum` array, where `prefixSum[i]` represents the sum of elements from the start to the `i`th index.

2. **Iterate for Splits**:  
   - For each potential split index \( i \), check if the sum of the first part \( `prefixSum[i+1]` \) is greater than or equal to the sum of the second part \( `prefixSum[n]`- `prefixSum[i+1]` \).  
   - Count such splits.

3. **Return Count**:  
   - Return the total number of valid splits.




### TestCase:  
`nums = [10, 4, -8, 7]`


### Step 1: Compute `prefixSum`

The prefix sum array stores the cumulative sum of elements in the array up to each index.  
- **Initialization**: `prefixSum` is a vector of size `nums.size() + 1`, initialized to all zeros.  



### Step 2: Check Valid Splits

For each split index \( i \), check if the sum of the first part (left) is greater than or equal to the sum of the second part (right).  
- **Condition**:  
  \[
  prefixSum[i + 1] >= prefixSum[n] - prefixSum[i + 1]
  \]  
  where \( n \) is the size of `nums`.

#### Iteration:
1. \( i = 0 \):  
   - Left Sum: \( prefixSum[1] = 10 \)  
   - Right Sum: \( prefixSum[4] - prefixSum[1] = 13 - 10 = 3 \)  
   - \( 10 >= 3 \): **Valid split**  
   - Increment `count` → `count = 1`.

2. \( i = 1 \):  
   - Left Sum: \( prefixSum[2] = 14 \)  
   - Right Sum: \( prefixSum[4] - prefixSum[2] = 13 - 14 = -1 \)  
   - \( 14 >= -1 \): **Valid split**  
   - Increment `count` → `count = 2`.

3. \( i = 2 \):  
   - Left Sum: \( prefixSum[3] = 6 \)  
   - Right Sum: \( prefixSum[4] - prefixSum[3] = 13 - 6 = 7 \)  
   - \( 6 not >= 7 \): **Not a valid split**  
   - `count` remains unchanged.



### Final Output

**Total valid splits**: `2`  
**Result**: `count = 2`

---

### Dry Run Summary

| Index \( i \) | Left Sum (\( prefixSum[i+1] \)) | Right Sum (\( prefixSum[n] - prefixSum[i+1] \)) | Condition ( >= \) | Valid Split |
|---------------|--------------------------------|-----------------------------------------------|------------------------|-------------|
| 0             | 10                             | 3                                             |  10 >= 3         | Yes         |
| 1             | 14                             | -1                                            |  14 >= -1        | Yes         |
| 2             | 6                              | 7                                             |  6 !>= 7      | No          |

**Output**: `2`
# Code ( Click on it !!!)
<details>
<summary><strong>C++</strong></summary>

```cpp
class Solution {
public:
    int waysToSplitArray(vector<int>& nums) {
        vector<long long> prefixSum(nums.size() + 1, 0);
        int count = 0;

        for (int i = 0; i < nums.size(); i++) {
            prefixSum[i + 1] = prefixSum[i] + nums[i];
        }

        for (int i = 0; i < nums.size() - 1; i++) {
            if (prefixSum[i + 1] >= prefixSum[nums.size()] - prefixSum[i + 1]) {
                count++;
            }
        }
        return count;
    }
};


</details>

<details>
<summary><strong>Python</strong></summary>

```python
class Solution:
    def waysToSplitArray(self, nums):
        prefixSum = [0] * (len(nums) + 1)
        count = 0

        for i in range(len(nums)):
            prefixSum[i + 1] = prefixSum[i] + nums[i]

        for i in range(len(nums) - 1):
            if prefixSum[i + 1] >= prefixSum[-1] - prefixSum[i + 1]:
                count += 1

        return count
```

</details>

<details>
<summary><strong>Java</strong></summary>

```java
class Solution {
    public int waysToSplitArray(int[] nums) {
        long[] prefixSum = new long[nums.length + 1];
        int count = 0;

        for (int i = 0; i < nums.length; i++) {
            prefixSum[i + 1] = prefixSum[i] + nums[i];
        }

        for (int i = 0; i < nums.length - 1; i++) {
            if (prefixSum[i + 1] >= prefixSum[nums.length] - prefixSum[i + 1]) {
                count++;
            }
        }

        return count;
    }
}
```

</details>

<details>
<summary><strong>JavaScript</strong></summary>

```javascript
var waysToSplitArray = function(nums) {
    const prefixSum = new Array(nums.length + 1).fill(0);
    let count = 0;

    for (let i = 0; i < nums.length; i++) {
        prefixSum[i + 1] = prefixSum[i] + nums[i];
    }

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

    return count;
};
```

</details>

<details>
<summary><strong>Go</strong></summary>

```go
func waysToSplitArray(nums []int) int {
    prefixSum := make([]int64, len(nums)+1)
    count := 0

    for i, num := range nums {
        prefixSum[i+1] = prefixSum[i] + int64(num)
    }

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

    return count
}
```

</details>

<details>
<summary><strong>Rust</strong></summary>

```rust
impl Solution {
    pub fn ways_to_split_array(nums: Vec<i32>) -> i32 {
        let mut prefix_sum = vec![0i64; nums.len() + 1];
        let mut count = 0;

        for i in 0..nums.len() {
            prefix_sum[i + 1] = prefix_sum[i] + nums[i] as i64;
        }

        for i in 0..nums.len() - 1 {
            if prefix_sum[i + 1] >= prefix_sum[nums.len()] - prefix_sum[i + 1] {
                count += 1;
            }
        }

        count
    }
}
```

</details>

# Complexity
- **Time Complexity**:  
  - Prefix sum calculation: \( O(n) \)  
  - Iterating through splits: \( O(n) \)  
  - Total: \( O(n) \)

- **Space Complexity**:  
  - \( O(n) \) for the prefix sum array.

Let's walk through the dry run for the input array `[10, 4, -8, 7]`, using the C++ implementation provided, and explain the process step by step.

<img src="https://media.tenor.com/WrG5Ar-qk5QAAAAM/upvote-give-this-man.gif" alt="Prefix Sum Visualization" style="max-width: 100%; width: 1000px; height: auto;">

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