Minimize Rounding Error to Meet Target - Problem

You're working as a financial analyst and need to adjust a list of prices to meet an exact budget target. Given an array of decimal prices [p1, p2, ..., pn] and a target sum, you must round each price to its nearest integer (either floor or ceiling) such that the total equals the target.

Your goal: Find the rounding strategy that minimizes the total rounding error while achieving the exact target sum.

The rounding error for each price is |rounded_price - original_price|. You want to minimize the sum of all rounding errors.

Return: The minimum total rounding error as a string with exactly 3 decimal places, or "-1" if it's impossible to reach the target.

Example: If prices = [0.7, 2.3, 4.9] and target = 8, you could round to [1, 2, 5] with total error = 0.3 + 0.3 + 0.1 = 0.700

Input & Output

example_1.py โ€” Basic Case
$ Input: [0.7, 2.3, 4.9] 8
โ€บ Output: 1.000
๐Ÿ’ก Note: We can round to [1, 2, 5] giving sum=8 and error=|1-0.7|+|2-2.3|+|5-4.9|=0.3+0.3+0.1=0.7. Or [1, 3, 4] giving error=0.3+0.7+0.9=1.9. The minimum is 1.000.
example_2.py โ€” Impossible Case
$ Input: [2.5, 3.5] 7
โ€บ Output: -1
๐Ÿ’ก Note: The maximum possible sum is ceil(2.5)+ceil(3.5)=3+4=7, but we'd need sum=7. Wait, this should be possible! Let me recalculate: we can get [3,4]=7. Error would be 0.5+0.5=1.000.
example_3.py โ€” Edge Case
$ Input: [1.0, 2.0, 3.0] 6
โ€บ Output: 0.000
๐Ÿ’ก Note: All numbers are already integers, so no rounding error. Sum is exactly 6.

Constraints

  • 1 โ‰ค prices.length โ‰ค 103
  • 0 โ‰ค prices[i] โ‰ค 104
  • 0 โ‰ค target โ‰ค 107
  • Each price has at most 3 digits after decimal point

Visualization

Tap to expand
๐Ÿ’ฐ Smart Budget Allocation Strategy๐Ÿข Department Requests vs AllocationMarketing DeptRequested: $2.8KFloor: $2KFractional: 0.8 (HIGH)IT DepartmentRequested: $4.3KFloor: $4KFractional: 0.3 (LOW)HR DepartmentRequested: $1.7KFloor: $1KFractional: 0.7 (MED)๐Ÿ“Š Allocation Strategy1. Baseline: Give floor amounts: $2K + $4K + $1K = $7K2. Target: Need $9K total, so $2K more to allocate3. Priority: Marketing (0.8 frac) > HR (0.7 frac) > IT (0.3 frac)4. Result: Give +$1K to Marketing and HR โ†’ [$3K, $4K, $2K]Total Error: 0.2 + 0.3 + 0.3 = 0.8 (minimized!)๐ŸŽฏ Final AllocationMarketing: $3K โœ“Error: |3-2.8| = 0.2IT: $4KError: |4-4.3| = 0.3HR: $2K โœ“Error: |2-1.7| = 0.3Total: $9K โœ“
Understanding the Visualization
1
Baseline Allocation
Give each department the floor of their request to establish minimum spend
2
Calculate Shortfall
Determine how much more money needed to reach target budget
3
Prioritize by Impact
Rank departments by how close they are to next dollar (fractional part)
4
Strategic Increases
Give extra dollars to departments where it minimizes disappointment
Key Takeaway
๐ŸŽฏ Key Insight: Greedily prioritize rounding up numbers with the largest fractional parts - they're closest to the next integer and cause minimal error when rounded up!
Asked in
Google 35 Amazon 28 Bloomberg 22 Microsoft 15
28.4K Views
Medium-High Frequency
~25 min Avg. Time
890 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