Detailed Explanation Total Sum | Prefix Sum 100%Beats
🌟 IntuitionThe task requires finding the number of ways to split the array such that the sum of the left part is greater than or equal to the sum of the right p
# 🌟 **Intuition**  
The task requires finding the number of ways to split the array such that the **sum of the left part is greater than or equal to the sum of the right part**.  

To solve this, consider:  
- **Brute force approach**: For every possible split, calculate the sum of the left and right parts. This is inefficient as it involves recalculating sums repeatedly for each split, leading to $$O(n^2)$$ time complexity.  
- **Optimized approach**: Use a **running sum** technique where:  
  1. Precompute the total sum of the array.
  2. Dynamically update the left and right sums as you traverse the array.  
This reduces redundant calculations and ensures a linear $$O(n)$$ solution.

**Subscribe For Video Solution Startind today : https://shorturl.at/zM3iG**

---

# 🛠️ **Approach**  
### 1️⃣ **Precompute the Total Sum**  
- Calculate the **total sum** of the array (`rightSideSum`). This represents the initial sum of the "right side" before any split.  

### 2️⃣ **Simulate Splits Efficiently**  
- Use two variables:
  - `leftSideSum` initialized to 0.
  - `rightSideSum` initialized to the total sum of the array.  
- Traverse the array:  
  - For each split at index `i`, update:  
    - Add `nums[i]` to `leftSideSum` (growing the left part).  
    - Subtract `nums[i]` from `rightSideSum` (shrinking the right part).  
  - Check the condition: **`leftSideSum >= rightSideSum`**.  

### 3️⃣ **Count Valid Splits**  
- For every valid split (where the condition is satisfied), increment the count.

---

# 📊 **Complexity**  
- **Time Complexity**:  
  $$O(n)$$ — Two passes: one for the total sum and one for simulating splits.  
- **Space Complexity**:  
  $$O(1)$$ — Only a few variables are used for computations.  

---

# 📋 **Example Walkthrough**  
Consider the input:  
`nums = [10, 4, 1, 3, 5]`  

### **Step 1: Compute Total Sum**  
- `rightSideSum = 10 + 4 + 1 + 3 + 5 = 23`  

### **Step 2: Simulate Splits**  
- Initialize `leftSideSum = 0` and `rightSideSum = 23`.  

| **Index** | **Left Side Sum** | **Right Side Sum** | **Condition (`>=`)?** | **Valid Split?** |  
|-----------|--------------------|--------------------|------------------------|------------------|  
| 0         | 10                 | 13                 | ❌ No                  | No               |  
| 1         | 14                 | 9                  | ✅ Yes                 | Yes              |  
| 2         | 15                 | 8                  | ✅ Yes                 | Yes              |  
| 3         | 18                 | 5                  | ✅ Yes                 | Yes              |  

- **Result**: 3 valid splits.  

---

# 🌈 **Detailed Steps**  

### **Step 1: Precompute the Total Sum**  
- Sum up all the elements in `nums` to get `rightSideSum`.  
- This value represents the entire array sum before any split.  

---

### **Step 2: Traverse and Simulate Splits**  
- Use a loop to iterate through each index except the last one (splits are made between elements).  
- For each index:  
  1. Add the current element to `leftSideSum`.  
  2. Subtract the current element from `rightSideSum`.  
  3. Check the condition: **`leftSideSum >= rightSideSum`**. If true, increment the count of valid splits.  

---

### **Step 3: Return the Count**  
- After the loop, return the total count of valid splits.  
### First Video Solution Show Some Support I Am Nervous ###
https://www.youtube.com/watch?v=43qs6v4Wtzo&ab_channel=Mr.Destroy

![Screenshot 2025-01-03 060054.png](https://assets.leetcode.com/users/images/f76cccb6-1139-4783-8688-d9bbc9cb47c6_1735864362.521856.png)



---

# 💻 **Code**  
#**Prefix Sume Method**
```C++ []
class Solution {
public:
    int waysToSplitArray(vector<int>& nums) {
        int N = nums.size();
        vector<long long> Prefix(N + 1, 0);
        for (int i = 0; i < N; i++) {
            Prefix[i + 1] = Prefix[i] + nums[i];
        }
        int ANS = 0;
        for (int i = 1; i < N; i++) {
            if (Prefix[i] >= Prefix[N] - Prefix[i]) {
                ANS++;
            }
        }
        return ANS;
    }
};
```
**Runing Sum Method**
```cpp []
class Solution {
public:
    int waysToSplitArray(vector<int>& nums) {
        long long leftSideSum = 0, rightSideSum = 0;
        for (int num : nums) {
            rightSideSum += num;
        }

        int validSplits = 0;

        for (int i = 0; i < nums.size() - 1; i++) {
            leftSideSum += nums[i];
            rightSideSum -= nums[i];

            if (leftSideSum >= rightSideSum) {
                validSplits++;
            }
        }

        return validSplits;
    }
};
```
```Java []
class Solution {
    public int waysToSplitArray(int[] nums) {
        long leftSum = 0, rightSum = 0;
        for (int num : nums) {
            rightSum += num;
        }

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

        return validSplits;
    }
}
```
```Python3 []
class Solution:
    def waysToSplitArray(self, nums: List[int]) -> int:
        leftSideSum = 0
        rightSideSum = sum(nums)
        validSplits = 0
        
        for i in range(len(nums) - 1):
            leftSideSum += nums[i]
            rightSideSum -= nums[i]
            if leftSideSum >= rightSideSum:
                validSplits += 1
                
        return validSplits
```
```C []
int waysToSplitArray(int* nums, int numsSize) {
    long long leftSum = 0, rightSum = 0;
    for (int i = 0; i < numsSize; i++) {
        rightSum += nums[i];
    }

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

    return validSplits;
}
```
```C# []
public class Solution {
    public int WaysToSplitArray(int[] nums) {
        long leftSideSum = 0, rightSideSum = 0;
        foreach (int num in nums) {
            rightSideSum += num;
        }

        int validSplits = 0;
        for (int i = 0; i < nums.Length - 1; i++) {
            leftSideSum += nums[i];
            rightSideSum -= nums[i];
            if (leftSideSum >= rightSideSum) {
                validSplits++;
            }
        }

        return validSplits;
    }
}
```
```java_script []
var waysToSplitArray = function(nums) {
    let leftSideSum = 0, rightSideSum = nums.reduce((acc, num) => acc + num, 0);
    let validSplits = 0;

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

    return validSplits;
};
```
```Ruby []
def ways_to_split_array(nums)
    leftSideSum = 0
    rightSideSum = nums.sum
    validSplits = 0

    (0...nums.length - 1).each do |i|
        leftSideSum += nums[i]
        rightSideSum -= nums[i]
        validSplits += 1 if leftSideSum >= rightSideSum
    end

    return validSplits
end
```

['Array', 'C', 'C++', 'Java', 'Python3', 'Ruby', 'JavaScript', 'C#']