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.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.
values=[7, 7, 7] → 0
At index 0 both averages are already 7, and ties go to the smallest index.
values=[4, 0, 5, 1]
1
Run checks the first 3 tests in your browser. Submit checks all 92 tests on the server, hidden ones included.