Watch 5 video solutions for Calculate Amount Paid in Taxes, a easy level problem involving Array, Simulation. This walkthrough by Programming Live with Larry has 775 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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:
upper0 dollars earned are taxed at a rate of percent0.upper1 - upper0 dollars earned are taxed at a rate of percent1.upper2 - upper1 dollars earned are taxed at a rate of percent2.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. Answers within 10-5 of the actual answer will be accepted.
Example 1:
Input: brackets = [[3,50],[7,10],[12,25]], income = 10 Output: 2.65000 Explanation: Based on your income, you have 3 dollars in the 1st tax bracket, 4 dollars in the 2nd tax bracket, and 3 dollars in the 3rd tax bracket. The tax rate for the three tax brackets is 50%, 10%, and 25%, respectively. In total, you pay $3 * 50% + $4 * 10% + $3 * 25% = $2.65 in taxes.
Example 2:
Input: brackets = [[1,0],[4,25],[5,50]], income = 2 Output: 0.25000 Explanation: Based on your income, you have 1 dollar in the 1st tax bracket and 1 dollar in the 2nd tax bracket. The tax rate for the two tax brackets is 0% and 25%, respectively. In total, you pay $1 * 0% + $1 * 25% = $0.25 in taxes.
Example 3:
Input: brackets = [[2,50]], income = 0 Output: 0.00000 Explanation: You have no income to tax, so you have to pay a total of $0 in taxes.
Constraints:
1 <= brackets.length <= 1001 <= upperi <= 10000 <= percenti <= 1000 <= income <= 1000upperi is sorted in ascending order.upperi are unique.income.Problem Overview: You are given progressive tax brackets where each bracket defines an upper income limit and the percentage tax applied to that portion of income. The task is to compute the total tax paid for a given income by applying each bracket sequentially until the income limit is reached.
Approach 1: Iterative Calculation of Taxes by Bracket (O(n) time, O(1) space)
This approach directly simulates how progressive taxes are calculated in real systems. Iterate through the brackets array and determine how much of your income falls inside the current bracket. For each bracket, compute the taxable portion as min(income, upperLimit) - previousLimit, multiply it by the tax percentage, and accumulate the result. Update the previous limit as you move to the next bracket. Once the income falls below the current bracket's upper limit, the remaining tax can be computed and the loop stops. This method uses only simple arithmetic and a single pass through the array, making it ideal for problems involving array traversal and direct simulation.
Approach 2: Cumulative Summation with Early Exit (O(n) time, O(1) space)
This variation also processes brackets sequentially but focuses on accumulating the taxable income incrementally. Instead of recalculating limits repeatedly, track the remaining income and process each bracket's capacity. For each bracket, determine how much income can be taxed inside that range and add the computed tax to a running total. If the remaining income becomes zero or negative, break early since no further brackets apply. The early exit avoids unnecessary iterations when income lies within lower brackets. The algorithm still runs in linear time relative to the number of brackets and requires constant extra memory.
Recommended for interviews: The iterative bracket simulation is the expected solution. Interviewers want to see that you correctly model progressive tax calculation and handle bracket boundaries without off‑by‑one mistakes. Both implementations run in O(n) time with O(1) space, but the direct bracket iteration clearly demonstrates your understanding of range processing and controlled iteration over arrays.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterative Calculation of Taxes by Bracket | O(n) | O(1) | Standard solution when simulating progressive tax brackets |
| Cumulative Summation with Early Exit | O(n) | O(1) | Useful when income often falls in lower brackets and early termination saves iterations |