Finding least number of notes to sum an amount - JavaScript

Suppose, we have a currency system where we have denominations of 1000 units, 500 units, 100 units, 50 units, 20 units, 10 units, 5 units, 2 units and 1 unit.

Given a specific amount, we are required to write a function that calculates the least number of total denominations that sum up to the amount.

For example, if the amount is 512,

The least number of notes that will add up to it will be:
1 unit of 500, 1 unit of 10 and 1 unit of 2.

So, for 512, our function should return 3, i.e., the total count of notes.

Using Greedy Algorithm Approach

The greedy algorithm works perfectly for this problem since we always pick the largest denomination that fits. Here's the implementation:

const sum = 512;
const countNotes = sum => {
    let count = 0;
    while(sum){
        if(sum >= 1000){
            sum -= 1000;
            count++;
            continue;
        }else if(sum >= 500){
            sum -= 500;
            count++;
            continue;
        }else if(sum >= 100){
            sum -= 100;
            count++;
            continue;
        }else if(sum >= 50){
            sum -= 50;
            count++;
            continue;
        }else if(sum >= 20){
            sum -= 20;
            count++;
            continue;
        }else if(sum >= 10){
            sum -= 10;
            count++;
            continue;
        }else if(sum >= 5){
            sum -= 5;
            count++;
            continue;
        }else if(sum >= 2){
            sum -= 2;
            count++;
            continue;
        }else{
            sum -= 1;
            count++;
            continue;
        }
    };
    return count;
};
console.log(countNotes(sum));
3

Optimized Array-Based Approach

We can make the code more maintainable using an array of denominations:

const countNotesOptimized = (amount) => {
    const denominations = [1000, 500, 100, 50, 20, 10, 5, 2, 1];
    let count = 0;
    
    for (let denomination of denominations) {
        while (amount >= denomination) {
            amount -= denomination;
            count++;
        }
    }
    
    return count;
};

// Test with different amounts
console.log(countNotesOptimized(512));  // 3
console.log(countNotesOptimized(1787)); // 10
console.log(countNotesOptimized(93));   // 6
3
10
6

Step-by-Step Breakdown for 512

Let's trace through how the algorithm works for amount 512:

const traceNotes = (amount) => {
    const denominations = [1000, 500, 100, 50, 20, 10, 5, 2, 1];
    let count = 0;
    
    console.log(`Breaking down amount: ${amount}`);
    
    for (let denomination of denominations) {
        let used = 0;
        while (amount >= denomination) {
            amount -= denomination;
            count++;
            used++;
        }
        if (used > 0) {
            console.log(`Used ${used} note(s) of ${denomination}`);
        }
    }
    
    console.log(`Total notes used: ${count}`);
    return count;
};

traceNotes(512);
Breaking down amount: 512
Used 1 note(s) of 500
Used 1 note(s) of 10
Used 1 note(s) of 2
Total notes used: 3

How It Works

The greedy algorithm works by always selecting the largest denomination that doesn't exceed the remaining amount. This approach guarantees the minimum number of notes because larger denominations reduce the remaining amount more efficiently.

Conclusion

The greedy algorithm provides an optimal solution for finding the minimum number of currency notes. The array-based approach offers better maintainability and can easily accommodate different currency systems.

Updated on: 2026-03-15T23:18:59+05:30

813 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements