Calculate Amount Paid in Taxes - Problem

You are given a 0-indexed 2D integer array brackets where brackets[i] = [upperi, percenti] means that the ith tax bracket has an upper bound of upperi and is taxed at a rate of percenti. The brackets are sorted by upper bound (i.e. upperi-1 < upperi for 0 < i < brackets.length).

Tax is calculated as follows:

  • The first upper0 dollars earned are taxed at a rate of percent0.
  • The next upper1 - upper0 dollars earned are taxed at a rate of percent1.
  • The next upper2 - upper1 dollars earned are taxed at a rate of percent2.
  • And so on.

You are given an integer income representing the amount of money you earned. Return the amount of money that you have to pay in taxes. Note: Answers within 10-5 of the actual answer will be accepted.

Input & Output

Example 1 — Basic Progressive Tax
$ Input: brackets = [[3,50],[7,10],[20,25]], income = 10
Output: 2.65
💡 Note: First $3 taxed at 50%: 3 × 0.5 = 1.5. Next $4 (income 4-7) taxed at 10%: 4 × 0.1 = 0.4. Last $3 (income 8-10) taxed at 25%: 3 × 0.25 = 0.75. Total: 1.5 + 0.4 + 0.75 = 2.65
Example 2 — Income Below Second Bracket
$ Input: brackets = [[1,0],[4,25],[5,50]], income = 2
Output: 0.25
💡 Note: First $1 taxed at 0%: 1 × 0.0 = 0. Next $1 (income $2) taxed at 25%: 1 × 0.25 = 0.25. Total: 0 + 0.25 = 0.25
Example 3 — Single Tax Bracket
$ Input: brackets = [[2,3]], income = 2
Output: 0.06
💡 Note: Entire income of $2 falls in first bracket taxed at 3%: 2 × 0.03 = 0.06

Constraints

  • 1 ≤ brackets.length ≤ 100
  • 1 ≤ upperi ≤ 1000
  • 0 ≤ percenti ≤ 100
  • 0 ≤ income ≤ 1000
  • upperi is sorted in ascending order
  • All the values of upperi are unique
  • The upper bound of the last tax bracket is greater than or equal to income

Visualization

Tap to expand
Progressive Tax System: Income = $10Brackets: [[3,50], [7,10], [20,25]]Bracket 1: [3, 50%]Income $1-$3Tax: 3 × 50% = $1.50Bracket 2: [7, 10%]Income $4-$7Tax: 4 × 10% = $0.40Bracket 3: [20, 25%]Income $8-$10Tax: 3 × 25% = $0.75$1.50$0.40$0.75++=Total Tax: $2.65Each bracket taxes its portion of income at its own rate
Understanding the Visualization
1
Input
Tax brackets with upper bounds and rates, plus total income
2
Process
Divide income across brackets and apply corresponding tax rates
3
Output
Total tax amount calculated from all applicable brackets
Key Takeaway
🎯 Key Insight: Process income by bracket ranges, not individual dollars, for efficient O(n) taxation calculation
Asked in
Amazon 15 Microsoft 8
28.0K Views
Medium Frequency
~15 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