Ctrl K

Best Cut Points

Medium
You are given an array bits of length n holding only zeros and ones. Cutting it at position i (0 ≤ i ≤ n) splits it into two parts: the left part has the elements at indices 0 through i - 1, the right part has indices i through n - 1. With i = 0 the left part is empty; with i = n the right part is. The score of a cut is the number of zeros in the left part plus the number of ones in the right part. Return every position i whose score is the highest. The positions may come in any order.
Example 1
bits=[1, 0, 1, 1, 0] → [0, 2]

The cuts 0…5 score 3, 2, 3, 2, 1, 2. The best score, 3, is reached at positions 0 and 2.

Example 2
bits=[0, 0, 1, 1] → [2]

Cutting right in the middle leaves every 0 on the left and every 1 on the right, for a score of 4.

Example 3
bits=[1, 0] → [0, 2]

All three cuts score 1, so 0, 1 and 2 all qualify.

Constraints
  • 1 ≤ n ≤ 10⁵
  • Each bits[i] is 0 or 1
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsTwo Boards Holding Water MediumZero-Sum Triples MediumTriple Sum Nearest the Target MediumFour Numbers to a Target MediumNext Arrangement in Order Medium
loading Python…
Input
bits=[1, 0, 1, 1, 0]
Expected
[0, 2]

Run checks the first 3 tests in your browser. Submit checks all 101 tests on the server, hidden ones included.