Watch 10 video solutions for Account Balance After Rounded Purchase, a easy level problem involving Math. This walkthrough by Optimization has 506 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Initially, you have a bank account balance of 100 dollars.
You are given an integer purchaseAmount representing the amount you will spend on a purchase in dollars, in other words, its price.
When making the purchase, first the purchaseAmount is rounded to the nearest multiple of 10. Let us call this value roundedAmount. Then, roundedAmount dollars are removed from your bank account.
Return an integer denoting your final bank account balance after this purchase.
Notes:
Example 1:
Input: purchaseAmount = 9
Output: 90
Explanation:
The nearest multiple of 10 to 9 is 10. So your account balance becomes 100 - 10 = 90.
Example 2:
Input: purchaseAmount = 15
Output: 80
Explanation:
The nearest multiple of 10 to 15 is 20. So your account balance becomes 100 - 20 = 80.
Example 3:
Input: purchaseAmount = 10
Output: 90
Explanation:
10 is a multiple of 10 itself. So your account balance becomes 100 - 10 = 90.
Constraints:
0 <= purchaseAmount <= 100Problem Overview: You start with a bank balance of 100. Given a purchase amount, the store rounds that amount to the nearest multiple of 10. After rounding, subtract the rounded value from the balance and return the remaining amount.
Approach 1: Direct Iterative Method (Time: O(1), Space: O(1))
The straightforward way is to determine which multiple of 10 is closest to the purchase amount. Compute the remainder using purchaseAmount % 10. If the remainder is 5 or greater, round up to the next multiple of 10; otherwise round down to the previous multiple. After calculating the rounded value, subtract it from 100 to get the final balance. This approach relies on simple arithmetic operations and conditional checks, making it the cleanest and fastest implementation. Since the operations are constant-time, the overall complexity stays O(1) with O(1) space.
This method fits naturally into problems categorized under math and small-scale simulation tasks where the logic mirrors a real-world rule.
Approach 2: Recursive Rounding Method (Time: O(1), Space: O(1))
A recursive variant computes the nearest multiple of 10 by progressively reducing the number until it reaches a base case (a multiple of 10). Each recursive step either increments or decrements the value based on the rounding rule until the correct boundary is found. After identifying the rounded amount, subtract it from the initial balance of 100. Because the purchase amount is small and the recursion depth is bounded by a single digit difference, the effective time complexity remains O(1), with constant auxiliary space for the call stack.
This approach is useful when practicing recursion patterns or understanding how rounding logic can be expressed through repeated function calls. It also connects conceptually with problems involving recursion and numeric transformations.
Recommended for interviews: The direct arithmetic method is what interviewers expect. It demonstrates that you recognize the rounding rule and translate it into constant-time math operations. Implementing a recursive solution can show conceptual flexibility, but the iterative math approach is clearer, shorter, and typically preferred in production code.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Direct Iterative Math | O(1) | O(1) | Best general solution; simple arithmetic rounding using modulo |
| Recursive Rounding | O(1) | O(1) | Useful for practicing recursion or expressing rounding through repeated function calls |