Kotlin | Rust
Join me on Telegramhttps://t.me/leetcode_daily_unstoppable/853Problem TLDRCount splits left_sum >= right_sum #medium #prefix_sumIntuitionPrefix sum can help sol
![1.webp](https://assets.leetcode.com/users/images/933d68e7-cb54-48f1-979c-be123fb9a303_1735902211.0337007.webp)

https://youtu.be/MjsYNhMxHnM

#### Join me on Telegram

https://t.me/leetcode_daily_unstoppable/853

#### Problem TLDR

Count splits left_sum >= right_sum #medium #prefix_sum

#### Intuition

Prefix sum can help solve this.

#### Approach

* careful with an `int` overflow
* this is not about the balance and con't be done in a single pass, as adding negative number decreases the sum, we should hold `left` and `right` part separately

#### Complexity

- Time complexity:
$$O(n)$$

- Space complexity:
$$O(1)$$

#### Code

```kotlin []

    fun waysToSplitArray(nums: IntArray): Int {
        var r = nums.sumOf { it.toLong() }; var l = 0L
        return (0..<nums.lastIndex).count {
            l += nums[it]; r -= nums[it]; l >= r
        }
    }

```
```rust []

    pub fn ways_to_split_array(nums: Vec<i32>) -> i32 {
        let (mut l, mut r) = (0, nums.iter().map(|&x| x as i64).sum());
        (0..nums.len() - 1).filter(|&i| {
            l += nums[i] as i64; r -= nums[i] as i64; l >= r 
        }).count() as _
    }

```

```c++ []

    int waysToSplitArray(vector<int>& nums) {
        int res = 0; long long r = reduce(begin(nums), end(nums), 0LL), l = 0;
        for (int i = 0; i < nums.size() - 1; ++i)
            res += (l += nums[i]) >= (r -= nums[i]);
        return res;
    }

```
['Prefix Sum', 'C++', 'Rust', 'Kotlin']