Account Balance After Rounded Purchase - Problem

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).

Input & Output

Example 1 — Basic Rounding Down
$ Input: purchaseAmount = 9
Output: 90
💡 Note: 9 is closer to 10 than to 0, so it rounds to 10. Final balance: 100 - 10 = 90
Example 2 — Rounding Up with Tie
$ Input: purchaseAmount = 15
Output: 80
💡 Note: 15 is exactly between 10 and 20, so it rounds up to 20. Final balance: 100 - 20 = 80
Example 3 — No Rounding Needed
$ Input: purchaseAmount = 10
Output: 90
💡 Note: 10 is already a multiple of 10, no rounding needed. Final balance: 100 - 10 = 90

Constraints

  • 0 ≤ purchaseAmount ≤ 100

Visualization

Tap to expand
Account Balance After Rounded Purchase INPUT BANK Initial Balance: $100 Purchase Amount: $9 purchaseAmount = 9 Integer input ALGORITHM STEPS 1 Get last digit 9 % 10 = 9 2 Check rounding 9 >= 5, round UP 3 Calculate rounded 9 + (10-9) = 10 4 Subtract from balance 100 - 10 = 90 roundedAmount = ((9 + 5) / 10) * 10 = (14 / 10) * 10 = 10 FINAL RESULT BANK 100 - 10 = 90 Final Balance: $90 Output: 90 Integer result OK - Verified Key Insight: Rounding to nearest 10: Add 5 to the number, then integer divide by 10, then multiply by 10. Formula: roundedAmount = ((purchaseAmount + 5) / 10) * 10. This handles the "5 rounds up" rule automatically. return 100 - roundedAmount; TutorialsPoint - Account Balance After Rounded Purchase | Optimal Solution
Asked in
Google 12 Amazon 8
15.0K Views
Medium Frequency
~5 min Avg. Time
450 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