🌟🧩 One Loop, Many Splits:💡 The Optimized Prefix Sum Trick!⚡🚀
Intuition 🤔We split the array into two 🧩 parts: left and right. The goal is to count how many splits make the left sum ≥ right sum. Easy, right? 🚀Approach 🛠️
To
# Intuition 🤔
We split the array into two 🧩 parts: left and right. The goal is to count how many splits make the left sum ≥ right sum. Easy, right? 🚀

---

# Approach 🛠️
1. Total Sum ➕: Compute the sum of all elements.
2. Iterate 🔄:
    - Start with leftSum = 0.
    - For each index i, add the current element to leftSum.
    - Calculate rightSum = totalSum - leftSum.
3. Check ✅: If leftSum >= rightSum, count it! 📊
4. Return: Boom! Output the count! 🎯

---

# Complexity ⚡
- Time ⏰: 
 
        O(n), since we loop once to compute the sums.
- Space 🪶:
  
        O(1), using only variables.

---

# Code
```cpp []
class Solution {
public:
    int waysToSplitArray(vector<int>& nums) {

/*
        // better approach..
        int n = nums.size();
        vector<long long> left (n, 0), right (n, 0);

        long long sum = 0;
        for(int i = 0; i < n; i++) {
            sum += nums[i];
            left[i] = sum;
        }
        sum = 0;
        for(int i = n-1; i >= 0; i--) {
            sum += nums[i];
            right[i] = sum;
        }

        int ans = 0;
        for(int i = 0; i < n-1; i++) {
            if(left[i] >= right[i+1]) ans++;
        }
        return ans;
*/

        // optimal approach..
        int n = nums.size();
        long long leftSum = 0, totalSum = 0;
        for(int val: nums) {
            totalSum += val;
        }

        int ans = 0;
        for(int i = 0; i < n-1; i++) {
            leftSum += nums[i];
            long long rightSum = totalSum - leftSum;

            if(leftSum >= rightSum) ans++;
        }
        return ans;
    }
};
```

```java []
class Solution {
    public int waysToSplitArray(int[] nums) {
        int n = nums.length;
        long totalSum = 0, leftSum = 0;

        for (int val : nums) {
            totalSum += val;
        }

        int ans = 0;
        for (int i = 0; i < n - 1; i++) {
            leftSum += nums[i];
            long rightSum = totalSum - leftSum;

            if (leftSum >= rightSum) {
                ans++;
            }
        }
        return ans;
    }
}

```
```python []
class Solution:
    def waysToSplitArray(self, nums):
        totalSum = sum(nums)
        leftSum = 0
        ans = 0

        for i in range(len(nums) - 1):
            leftSum += nums[i]
            rightSum = totalSum - leftSum

            if leftSum >= rightSum:
                ans += 1

        return ans

```
```javascript []
class Solution {
    waysToSplitArray(nums) {
        let totalSum = nums.reduce((a, b) => a + b, 0);
        let leftSum = 0, ans = 0;

        for (let i = 0; i < nums.length - 1; i++) {
            leftSum += nums[i];
            let rightSum = totalSum - leftSum;

            if (leftSum >= rightSum) {
                ans++;
            }
        }
        return ans;
    }
}

```
```ruby []
class Solution
    def ways_to_split_array(nums)
        total_sum = nums.sum
        left_sum = 0
        ans = 0

        (0...nums.length - 1).each do |i|
            left_sum += nums[i]
            right_sum = total_sum - left_sum

            ans += 1 if left_sum >= right_sum
        end

        ans
    end
end

```
```rust []
impl Solution {
    pub fn ways_to_split_array(nums: Vec<i32>) -> i32 {
        let total_sum: i64 = nums.iter().map(|&x| x as i64).sum();
        let mut left_sum: i64 = 0;
        let mut ans = 0;

        for i in 0..nums.len() - 1 {
            left_sum += nums[i] as i64;
            let right_sum = total_sum - left_sum;

            if left_sum >= right_sum {
                ans += 1;
            }
        }

        ans
    }
}

```
```go []
func waysToSplitArray(nums []int) int {
    totalSum := 0
    for _, val := range nums {
        totalSum += val
    }

    leftSum, ans := 0, 0
    for i := 0; i < len(nums)-1; i++ {
        leftSum += nums[i]
        rightSum := totalSum - leftSum

        if leftSum >= rightSum {
            ans++
        }
    }

    return ans
}

```
```c# []
public class Solution {
    public int WaysToSplitArray(int[] nums) {
        long totalSum = 0, leftSum = 0;
        foreach (int val in nums) {
            totalSum += val;
        }

        int ans = 0;
        for (int i = 0; i < nums.Length - 1; i++) {
            leftSum += nums[i];
            long rightSum = totalSum - leftSum;

            if (leftSum >= rightSum) {
                ans++;
            }
        }
        return ans;
    }
}

```
```typescript []
class Solution {
    waysToSplitArray(nums: number[]): number {
        let totalSum = nums.reduce((a, b) => a + b, 0);
        let leftSum = 0, ans = 0;

        for (let i = 0; i < nums.length - 1; i++) {
            leftSum += nums[i];
            let rightSum = totalSum - leftSum;

            if (leftSum >= rightSum) {
                ans++;
            }
        }
        return ans;
    }
}

```
```dart []
class Solution {
  int waysToSplitArray(List<int> nums) {
    int totalSum = nums.reduce((a, b) => a + b);
    int leftSum = 0, ans = 0;

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

      if (leftSum >= rightSum) {
        ans++;
      }
    }

    return ans;
  }
}

```
```swift []
class Solution {
    func waysToSplitArray(_ nums: [Int]) -> Int {
        let totalSum = nums.reduce(0, +)
        var leftSum = 0
        var ans = 0

        for i in 0..<nums.count - 1 {
            leftSum += nums[i]
            let rightSum = totalSum - leftSum

            if leftSum >= rightSum {
                ans += 1
            }
        }

        return ans
    }
}

```
['Swift', 'Prefix Sum', 'Python', 'C++', 'Java', 'Go', 'Rust', 'Ruby', 'JavaScript', 'Dart']