#2806
Easy Algorithms Account balance after rounded purchase
Math
56.3% acceptance
Feb 25, 2026
292
50
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:
0 is considered to be a multiple of 10 in this problem.
When rounding, 5 is rounded upward (5 is rounded to 10, 15 is rounded to 20, 25 to 30, and so on).
Solution
Rust
Time O(1)
Space O(1)
impl Solution {
pub fn account_balance_after_purchase(purchase_amount: i32) -> i32 {
let rounded = ((purchase_amount + 5) / 10) * 10;
100 - rounded
}
}