Skip to main content

Account Balance After Rounded Purchase - Solution & Explanation

EasyMath11 min read
Practice this problem

Problem Statement

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

 

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 <= 100

Approach Overview

Problem 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 1: Approach using Direct Iterative Methods

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), Space Complexity: O(1)

Try this approach in the editor →

Approach 2: Approach using Recursive Methods

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.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), Space Complexity: O(n) due to the call stack

Try this approach in the editor →

Approach 3: Enumeration + Simulation

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

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
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—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Direct Iterative MathO(1)O(1)Best general solution; simple arithmetic rounding using modulo
Recursive RoundingO(1)O(1)Useful for practicing recursion or expressing rounding through repeated function calls

Video Solution

Leetcode | 2806. Account Balance After Rounded Purchase | Easy | Java Solution • Developer Docs • 530 views views

Watch 9 more video solutions →

Frequently Asked Questions

Is Account Balance After Rounded Purchase easy or hard?
Account Balance After Rounded Purchase is classified as an Easy problem. It tests basic arithmetic reasoning, rounding rules, and clean implementation rather than complex algorithms or data structures.
Account Balance After Rounded Purchase Python/Java solution
In Python or Java, compute purchaseAmount % 10 to determine the rounding direction. Adjust the value to the nearest multiple of 10 and return 100 minus that rounded number. The implementation takes only a few lines and runs in O(1) time.
How to solve Account Balance After Rounded Purchase in O(1)?
Calculate the remainder using purchaseAmount % 10. If the remainder is at least 5, add (10 - remainder) to round up; otherwise subtract the remainder to round down. Finally compute 100 minus the rounded purchase amount to get the remaining balance. The operations are constant time.
What is the best approach for Account Balance After Rounded Purchase?
The best approach uses direct arithmetic rounding. Compute purchaseAmount % 10 to determine the nearest multiple of 10, round up when the remainder is 5 or more, then subtract the rounded value from 100. This runs in O(1) time and O(1) space and is the expected interview solution.
Is Account Balance After Rounded Purchase asked at Google/Amazon/Meta?
Account Balance After Rounded Purchase is typically categorized as an easy math or simulation problem. While not a common direct interview question at Google, Amazon, or Meta, similar rounding and arithmetic logic questions frequently appear in online assessments and coding screens.
What data structure is used in Account Balance After Rounded Purchase?
No specialized data structure is required. The problem relies purely on arithmetic operations such as modulo and conditional checks, making it a basic math and simulation problem.
What is the time complexity of Account Balance After Rounded Purchase?
The optimal solution runs in O(1) time because it performs a constant number of arithmetic operations and condition checks. Space complexity is also O(1) since no additional data structures are required.

Ready to solve this problem?

Practice Account Balance After Rounded Purchase with our built-in code editor and test cases.

Practice on FleetCode