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
upper0dollars earned are taxed at a rate ofpercent0. - The next
upper1 - upper0dollars earned are taxed at a rate ofpercent1. - The next
upper2 - upper1dollars earned are taxed at a rate ofpercent2. - 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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code