Balance Integer Values to Minimize Spread
Math & Bit ManipulationAuto-graded · 24 test casesLast updated
Tests understanding of integer division, remainder distribution, and handling negative numbers in a fairness optimization problem.
You are given a list of integers representing values that can be positive, negative, or zero. Your task is to balance these values so that the spread between the largest and smallest result is minimized, while keeping the total sum unchanged.
The optimal balance distributes the total as evenly as possible: some values will be ⌊total/n⌋ and others will be ⌊total/n⌋ + 1, where n is the number of values.
Return a list of the balanced values in non-decreasing order.
Constraints
- 0 ≤ len(values) ≤ 10⁵
- -10⁹ ≤ values[i] ≤ 10⁹
- The sum of all values fits in a 64-bit signed integer
- The output must preserve the total sum exactly
- Return an empty list if the input is empty
Examples
Example 1
Input: balance_values([10, 0, 0])
Output: [3, 3, 4]
Example 2
Input: balance_values([1, 2, 3, 4])
Output: [2, 2, 3, 3]
Example 3
Input: balance_values([5, -5, 10])
Output: [3, 3, 4]
Example 4
Input: balance_values([7, 7, 7])
Output: [7, 7, 7]
Hints
Hint 1
The total sum must remain constant after redistribution. How many values can you have at each level?
Hint 2
If the total doesn't divide evenly by n, exactly (total mod n) values must be one unit higher than the rest.
Hint 3
Python's floor division handles negative numbers correctly: -7 // 3 = -3, with remainder 2. The remainder is always non-negative.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
To minimize the spread while preserving the total sum, we need every value to be as close to the mean as possible. Since we must use integers, the best we can do is split values between two adjacent integers: ⌊total/n⌋ and ⌊total/n⌋ + 1.
The key insight is determining how many values should be at each level:
- Let
lower = total // n(floor division) - Let
remainder = total - lower * n
This remainder tells us how many values must be incremented by 1 to account for the leftover sum. Exactly remainder values will be lower + 1, and the remaining n - remainder values will be lower.
Why this is correct:
- Sum is conserved:
lower × (n - remainder) + (lower + 1) × remainder = lower × n + remainder = total - Spread is minimized: if total divides evenly, everyone gets the exact mean (spread = 0). Otherwise, a spread of 1 is the theoretical minimum for integer values
- Works for negative totals: Python's floor division always produces a non-negative remainder in [0, n)
Edge cases:
- Empty input: return empty list immediately to avoid division by zero
- Single value: remains unchanged
- Total of zero: all values become zero
- Negative totals: floor division handles correctly (e.g., -7 // 3 = -3, remainder = 2, giving [-3, -2, -2])
Complexity
- Time: O(n) to compute the sum and construct the output list
- Space: O(n) for the output list; O(1) auxiliary space
Solution
from typing import List
def balance_values(values: List[int]) -> List[int]:
n = len(values)
if n == 0:
return []
total = sum(values)
lower = total // n
remainder = total - lower * n
return [lower] * (n - remainder) + [lower + 1] * remainder