Skip to main content
Back to problems
#2155
Medium Algorithms

All divisions with the highest score of a binary array

Array
65.5% acceptance
Feb 25, 2026
538
17
You are given a 0-indexed binary array nums of length n. nums can be divided at index i into numsleft (indices 0..i-1) and numsright (indices i..n-1). The division score of index i is: count of 0s in numsleft + count of 1s in numsright. Return all distinct indices that have the highest possible division score.

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn max_score_indices(nums: Vec<i32>) -> Vec<i32> {
    let n = nums.len();
    let total_ones: i32 = nums.iter().sum();
    let mut zeros_left = 0i32;
    let mut ones_right = total_ones;
    let mut best = -1i32;
    let mut result = Vec::new();
    for i in 0..=n {
      let score = zeros_left + ones_right;
      if score > best {
        best = score;
        result.clear();
        result.push(i as i32);
      } else if score == best {
        result.push(i as i32);
      }
      if i < n {
        if nums[i] == 0 {
          zeros_left += 1;
        } else {
          ones_right -= 1;
        }
      }
    }
    result
  }
}