All Divisions With the Highest Score of a Binary Array - Problem

You are given a 0-indexed binary array nums of length n. nums can be divided at index i (where 0 ≤ i ≤ n) into two arrays (possibly empty) numsleft and numsright:

numsleft has all the elements of nums between index 0 and i - 1 (inclusive), while numsright has all the elements of nums between index i and n - 1 (inclusive).

If i == 0, numsleft is empty, while numsright has all the elements of nums.

If i == n, numsleft has all the elements of nums, while numsright is empty.

The division score of an index i is the sum of the number of 0's in numsleft and the number of 1's in numsright.

Return all distinct indices that have the highest possible division score. You may return the answer in any order.

Input & Output

Example 1 — Basic Case
$ Input: nums = [0,0,1,1,1]
Output: [2]
💡 Note: Division at index 2: left=[0,0] has 2 zeros, right=[1,1,1] has 3 ones, score=2+3=5 (maximum)
Example 2 — Multiple Answers
$ Input: nums = [0,0,0]
Output: [3]
💡 Note: Division at index 3: left=[0,0,0] has 3 zeros, right=[] has 0 ones, score=3+0=3 (maximum)
Example 3 — All Ones
$ Input: nums = [1,1]
Output: [0]
💡 Note: Division at index 0: left=[] has 0 zeros, right=[1,1] has 2 ones, score=0+2=2 (maximum)

Constraints

  • 1 ≤ nums.length ≤ 105
  • nums[i] is either 0 or 1

Visualization

Tap to expand
Binary Array Division Scoring ProblemInput Array:00111Division at i=2:LEFT: [0,0]RIGHT: [1,1,1]Count 0s: 2Count 1s: 3Score = Zeros in Left + Ones in RightScore at i=2: 2 + 3 = 5Maximum possible score!Output: [2]
Understanding the Visualization
1
Input
Binary array [0,0,1,1,1] with possible division points
2
Scoring
Count 0s in left part + 1s in right part for each division
3
Output
Return all indices with maximum division score
Key Takeaway
🎯 Key Insight: We want zeros on the left and ones on the right to maximize the score
Asked in
Google 45 Amazon 35 Microsoft 30 Apple 25
38.4K Views
Medium Frequency
~15 min Avg. Time
865 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen