Ctrl K

Most Balanced Split

Medium
You get an array of non-negative integers values of length n. Cut it after index i: the left part holds elements 0 through i, the right part holds everything after. For each part, take the average rounded down (the sum, integer-divided by the count). An empty right part has an average of 0. The **imbalance** of the cut is the absolute difference of the two averages. Return the index i with the smallest imbalance. If several indices tie, return the smallest one.
Example 1
values=[4, 0, 5, 1] → 1

Cuts: after index 0 gives |4 - 6 // 3| = 2; after 1 gives |4 // 2 - 6 // 2| = 1; after 2 gives |9 // 3 - 1| = 2; after 3 gives |10 // 4 - 0| = 2. The best is index 1.

Example 2
values=[7, 7, 7] → 0

At index 0 both averages are already 7, and ties go to the smallest index.

Constraints
  • 1 ≤ n = len(values) ≤ 10⁵
  • 0 ≤ values[i] ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsShortest Stretch to the Goal MediumProduct of All the Others MediumStudios for Recording Sessions MediumLongest Run with a Given Sum MediumBatch of Segment Adjustments Medium
loading Python…
Input
values=[4, 0, 5, 1]
Expected
1

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