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
Tax Calculator - Bracket Processing INPUT brackets = [[3,50],[7,10],[20,25]] [3, 50] upper: $3 rate: 50% [7, 10] upper: $7 rate: 10% [20, 25] upper: $20 rate: 25% INCOME $10 Income Range Breakdown $0-3 $3-7 $7-10 $10-20 (unused) 50% 10% 25% Input Values brackets: [[3,50],[7,10],[20,25]] income: 10 ALGORITHM STEPS 1 Initialize tax = 0 prev_upper = 0 2 Process Bracket [3,50] range = min(10,3) - 0 = 3 tax += 3 * 0.50 = 1.50 3 Process Bracket [7,10] range = min(10,7) - 3 = 4 tax += 4 * 0.10 = 0.40 4 Process Bracket [20,25] range = min(10,20) - 7 = 3 tax += 3 * 0.25 = 0.75 Tax Calculation Summary $0-$3: 3 x 50% = $1.50 $3-$7: 4 x 10% = $0.40 $7-$10: 3 x 25% = $0.75 Total: $2.65 FINAL RESULT TAX OWED $2.65 Output 2.65 OK - Verified Tax Breakdown $1.50 (57%) $0.40 (15%) $0.75 (28%) Key Insight: Process each bracket sequentially, calculating tax only for the income portion within that bracket. For each bracket: taxable_amount = min(income, upper) - previous_upper. Stop when income is fully processed. Time Complexity: O(n) where n = number of brackets. Space Complexity: O(1) - only tracking running total. TutorialsPoint - Calculate Amount Paid in Taxes | Optimized - Process by Brackets
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