BEATS 100%❤️‍🔥DRY-RUM❤️‍🔥SLIGHTLY AROUSING🤤
IntuitionThe problem requires splitting the 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 p


#### **Intuition**  
The problem requires splitting the 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. To achieve this, we can use prefix sums to efficiently calculate the sum of the left and right parts as we iterate through the array.  

- As we traverse the array, we maintain a running sum (`currSum`) for the left part.  
- The right part's sum can be calculated as the difference between the total sum of the array (`totalSum`) and the current running sum (`currSum`).  
- By comparing `currSum` with `remainingSum` at each split point, we count valid splits.

---

#### **Approach**  

1. **Calculate the Total Sum:**  
   Compute the total sum of the array to allow efficient computation of the right part's sum.  

2. **Iterate Through the Array:**  
   For each index `i` (excluding the last index to ensure both parts are non-empty):  
   - Add the current element to the running sum (`currSum`).  
   - Calculate the right part's sum as `totalSum - currSum`.  
   - If `currSum >= remainingSum`, increment the result count.  

3. **Return the Count:**  
   After traversing the array, return the total count of valid splits.  

---

#### **Dry Run**

Example Input: `nums = [10, 4, -8, 7]`  

| Step | `currSum` | `remainingSum` | Comparison `currSum >= remainingSum` | Result Count |
|------|-----------|----------------|---------------------------------------|--------------|
| Start| `0`       | `13`           | N/A                                   | `0`          |
| `i=0`| `10`      | `3`            | `10 >= 3` (True)                     | `1`          |
| `i=1`| `14`      | `-1`           | `14 >= -1` (True)                    | `2`          |
| `i=2`| `6`       | `7`            | `6 >= 7` (False)                     | `2`          |

**Output:** `2`  

---

#### **Code**

```java
class Solution {
    public int waysToSplitArray(int[] nums) {
        // Calculate the total sum of the array
        long totalSum = 0;
        for (int num : nums) {
            totalSum += num;
        }

        // Initialize variables to track the count of valid splits and the running sum
        int res = 0;
        long currSum = 0;

        // Iterate through the array up to the second last index
        for (int i = 0; i < nums.length - 1; i++) {
            currSum += nums[i]; // Update the running sum for the left part

            long remainingSum = totalSum - currSum; // Calculate the right part's sum

            // Check if the left sum is greater than or equal to the right sum
            if (currSum >= remainingSum) {
                res++; // Increment the count of valid splits
            }
        }

        return res; // Return the total count of valid splits
    }
}
```

---

#### **Complexity Analysis**

1. **Time Complexity:**  
   - Computing the total sum: O(n).  
   - Iterating through the array: O(n).  
   - Overall: **O(n)**.

2. **Space Complexity:**  
   - No additional data structures are used apart from a few variables: **O(1)**.

---

![Screenshot from 2024-07-09 09-56-34.png](https://assets.leetcode.com/users/images/4f72a0cf-fe09-45d7-ae9e-738f29f555c4_1735890002.7045963.png)

['Array', 'Suffix Array', 'Prefix Sum', 'Java']