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.
This approach involves solving the problem using basic iterative methods. We explore the problem using straightforward loops and conditions, which leads to an easy-to-understand solution.
This C code iteratively prints the elements of the array. The exampleFunction iterates through the array using a for loop. Each element is accessed using the loop index and printed using the printf function.
Time Complexity: O(n), Space Complexity: O(1)
This approach solves the problem using recursion. Recursive methods break down the problem into smaller subproblems by calling the same function within itself.
This C recursive function continues calling itself with the next index until reaching the end of the array. Only one element is processed per call, leading to a sequence of recursive calls.
Time Complexity: O(n), Space Complexity: O(n) due to the call stack
We enumerate all multiples of 10 within the range [0, 100], and find the one that is closest to purchaseAmount, denoted as x. The answer is 100 - x.
The time complexity is O(1), and the space complexity is O(1).
Python
Java
C++
Go
TypeScript
| Approach | Complexity |
|---|---|
| Approach using Direct Iterative Methods | Time Complexity: O(n), Space Complexity: O(1) |
| Approach using Recursive Methods | Time Complexity: O(n), Space Complexity: O(n) due to the call stack |
| Enumeration + Simulation | — |
| 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 |
6990. Account Balance After Rounded Purchase | Leetcode Biweekly Contest 110 | Sol Code In Comment. • Optimization • 506 views views
Watch 9 more video solutions →Practice Account Balance After Rounded Purchase with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor