Skip to content

How to Fix Minimum Coins Errors

DodaTech Updated 2026-06-26 1 min read

In this tutorial, you'll learn about How to Fix Minimum Coins Errors. We cover key concepts, practical examples, and best practices.

Fix minimum coins errors when coin denominations not sorted in descending order or greedy fails for non-canonical systems.

Quick Fix

Wrong

def min_coins(coins,amt):
    coins.sort()  # ascending gives largest coin last
    count=0
    for c in coins:
        count+=amt//c; amt%=c
    return count

Ascending sort processes smallest first. Uses many small coins instead of fewer large ones.

def min_coins(coins,amt):
    coins.sort(reverse=True)
    count=0
    for c in coins:
        if amt==0: break
        count+=amt//c; amt%=c
    return count if amt==0 else -1
coins=[1,5,10,25], amt=63 -> 6 (25*2+10*1+1*3). O(n log n).

Prevention

Sort descending. Use as many of each coin as possible. Check if exact amount reachable.

DodaTech Tools

Doda Browser's algorithm visualizer steps through DSA operations line by line. DodaZIP archives implementation patterns for team sharing. Durga Antivirus Pro detects memory corruption patterns in algorithm implementations.

FAQ

What is min coins greedy?

Minimum coins with unlimited supply. Works for canonical coin systems (USD, EUR).

Non-canonical?

Greedy fails for denominations like [1,3,4], amt=6. Greedy gives 4+1+1 (3), optimal 3+3 (2). Use DP.

When greedy works?

Canonical coin systems where each coin is a multiple of the next.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro