Skip to main content
Back to problems
#2270
Medium Algorithms

Number of ways to split array

Array Prefix Sum
55.9% acceptance
Feb 25, 2026
1195
100
You are given a 0-indexed integer array nums of length n. nums contains a valid split at index i if the following are true: The sum of the first i + 1 elements is greater than or equal to the sum of the last n - i - 1 elements. There is at least one element to the right of i. That is, 0 <= i < n - 1. Return the number of valid splits in nums.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn ways_to_split_array(nums: Vec<i32>) -> i32 {
    let total: i64 = nums.iter().map(|&x| x as i64).sum();
    let mut prefix = 0i64;
    let mut count = 0;
    for i in 0..nums.len()-1 {
      prefix += nums[i] as i64;
      if prefix >= total - prefix {
        count += 1;
      }
    }
    count
  }
}