Talentd
FleetCode
Make and follow sheets.Sign in

Account Balance After Rounded Purchase solution and explanation

EasyMath5 min read58.6% acceptanceUpdated 9 Apr 2026
Leetcode | 2806. Account Balance After Rounded Purchase | Easy | Java SolutionDeveloper Docs · 4:23 · 530 viewsWatch on YouTube
9 more videos on this problem
  1. Leetcode | 2806. Account Balance After Rounded Purchase | Easy | Java SolutionDeveloper Docs · 4:23 · 530 views
  2. 6990. Account Balance After Rounded Purchase | Leetcode Biweekly Contest 110 | Sol Code In Comment.Optimization · 0:15 · 506 views
  3. 2806. Account Balance After Rounded Purchase (Leetcode Easy)Programming Live with Larry · 2:32 · 225 views
  4. Leetcode BiWeekly contest 110 - Easy - Account Balance After Rounded PurchasePrakhar Agrawal · 8:20 · 175 views
  5. Account Balance After Rounded Purchase | Leetcode 2806 | Math | C++Tech Courses · 2:55 · 154 views
  6. Account Balance After Rounded Purchase | LeetCode 2806 | CppCodeClips with Abhishek Ranjan · 4:56 · 130 views
  7. Leetcode 2806. Account Balance After Rounded PurchaseLetsCode · 7:09 · 129 views
  8. 2806. Account Balance After Rounded Purchase || Leetcode bi weekly contest 110 || CppCS warriors · 2:52 · 93 views
  9. my attempt at solving leetcode 2806: account balance after rounded purchase [SOLVED]Cameron Late · 7:12 · 71 views
  10. Money-Saving Code: LeetCode 2806. Account Balance After Rounded Purchase Live CodingvanAmsen · 4:21 · 34 views

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

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.

def example_function(arr):
    for element in arr:
        print(element, end=' ')

arr = [1, 2, 3, 4, 5]
example_function(arr)
public class Example {
    public static void exampleFunction(int[] arr) {
        for (int value : arr) {
            System.out.print(value + " ");
        }
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        exampleFunction(arr);
    }
}
#include <iostream>

void exampleFunction(int arr[], int n) {
    for (int i = 0; i < n; i++) {
        std::cout << arr[i] << " ";
    }
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    exampleFunction(arr, n);
    return 0;
}
function exampleFunction(arr) {
    arr.forEach(function(element) {
        process.stdout.write(element + ' ');
    });
}

const arr = [1, 2, 3, 4, 5];
exampleFunction(arr);
#include <stdio.h>

void exampleFunction(int arr[], int n) {
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int n = sizeof(arr) / sizeof(arr[0]);
    exampleFunction(arr, n);
    return 0;
}
using System;

class Example {
    public static void ExampleFunction(int[] arr) {
        foreach (int value in arr) {
            Console.Write(value + " ");
        }
    }

    static void Main() {
        int[] arr = {1, 2, 3, 4, 5};
        ExampleFunction(arr);
    }
}

This Python code utilizes a for loop to access each element in the list and prints it using the print function with the end parameter to avoid new lines.

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.

def example_function(arr, index=0):
    if index >= len(arr):
        return
    print(arr[index], end=' ')
    example_function(arr, index + 1)

arr = [1, 2, 3, 4, 5]
example_function(arr)
public class Example {
    public static void exampleFunction(int[] arr, int index) {
        if (index >= arr.length) return;
        System.out.print(arr[index] + " ");
        exampleFunction(arr, index + 1);
    }

    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 5};
        exampleFunction(arr, 0);
    }
}
#include <iostream>

void exampleFunction(int arr[], int n, int index) {
    if (index >= n) return;
    std::cout << arr[index] << " ";
    exampleFunction(arr, n, index + 1);
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    exampleFunction(arr, sizeof(arr) / sizeof(arr[0]), 0);
    return 0;
}
function exampleFunction(arr, index = 0) {
    if (index >= arr.length) return;
    process.stdout.write(arr[index] + ' ');
    exampleFunction(arr, index + 1);
}

const arr = [1, 2, 3, 4, 5];
exampleFunction(arr);
#include <stdio.h>

void exampleFunction(int arr[], int n, int index) {
    if (index >= n) return;
    printf("%d ", arr[index]);
    exampleFunction(arr, n, index + 1);
}

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    exampleFunction(arr, sizeof(arr) / sizeof(arr[0]), 0);
    return 0;
}
using System;

class Example {
    public static void ExampleFunction(int[] arr, int index) {
        if (index >= arr.Length) return;
        Console.Write(arr[index] + " ");
        ExampleFunction(arr, index + 1);
    }

    static void Main() {
        int[] arr = {1, 2, 3, 4, 5};
        ExampleFunction(arr, 0);
    }
}

This Python recursion traverses through the list, calling itself with an increasing index till the end of the list. Each item is printed before recursive calls for the next element.

Complexity

Time Complexity: O(n), Space Complexity: O(n) due to recursion stack usage

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

function accountBalanceAfterPurchase(purchaseAmount: number): number {
    let [diff, x] = [100, 0];
    for (let y = 100; y >= 0; y -= 10) {
        const t = Math.abs(y - purchaseAmount);
        if (t < diff) {
            diff = t;
            x = y;
        }
    }
    return 100 - x;
}
func accountBalanceAfterPurchase(purchaseAmount int) int {
	diff, x := 100, 0
	for y := 100; y >= 0; y -= 10 {
		t := abs(y - purchaseAmount)
		if t < diff {
			diff = t
			x = y
		}
	}
	return 100 - x
}

func abs(x int) int {
	if x < 0 {
		return -x
	}
	return x
}
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 recursion stack usage

Enumeration + Simulation–

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

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.

Comments

Now write it yourselfClose this page and solve Account Balance After Rounded Purchase in the editor, without looking.
Practise Account Balance After Rounded Purchase