Find the fewest ATM bills that add up to a withdrawal amount

Quick Overview

Given a withdrawal amount and a set of bill denominations with unlimited supply, find the fewest bills that add up to the amount exactly, or report that it cannot be made. Tests dynamic programming over amounts and the reasoning for why taking the largest bill first can fail.

Find the fewest ATM bills that add up to a withdrawal amount

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an integer `amount` representing the total money to withdraw and an array of integers `denominations` representing the available bill types in the ATM, return the **minimum number of bills** needed to make up that amount. If the amount cannot be formed using any combination of the given denominations, return `-1`. Each denomination can be used an **unlimited** number of times. ### Function Signature ```python def min_bills(amount: int, denominations: list[int]) -> int: ``` ### Rules - `denominations` can arrive in any order. - A denomination larger than `amount` may appear in the input. It cannot be used, but it is not an error. ### Constraints - `1 <= amount <= 10^6` - `1 <= denominations.length <= 20` - `1 <= denominations[i] <= 10^6` - All values in `denominations` are unique. ### Examples ```text Input: amount = 230, denominations = [100, 50, 20, 10, 5, 1] Output: 4 ``` Two \$100 bills, one \$20 bill and one \$10 bill make 230 with 4 bills. Three bills cannot reach 230: two \$100 bills leave 30, which no single bill covers, and any three bills with at most one \$100 among them sum to at most 200. ```text Input: amount = 3, denominations = [2] Output: -1 ``` Any number of \$2 bills adds up to an even amount, so 3 cannot be formed. ```text Input: amount = 6, denominations = [4, 3, 1] Output: 2 ``` Two \$3 bills make 6. Taking the largest bill first gives \$4 + \$1 + \$1, which uses 3 bills, so the largest-first choice is not always the minimum.

Overview: Given a withdrawal amount and a set of bill denominations with unlimited supply, find the fewest bills that add up to the amount exactly, or report that it cannot be made. Tests dynamic programming over amounts and the reasoning for why taking the largest bill first can fail.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given an integer amount representing the total money to withdraw and an array of integers denominations representing the available bill types in the ATM, return the minimum number of bills needed to make up that amount.

If the amount cannot be formed using any combination of the given denominations, return -1.

Each denomination can be used an unlimited number of times.

Function Signature

def min_bills(amount: int, denominations: list[int]) -> int:

Rules

  • denominations can arrive in any order.
  • A denomination larger than amount may appear in the input. It cannot be used, but it is not an error.

Constraints

  • 1 <= amount <= 10^6
  • 1 <= denominations.length <= 20
  • 1 <= denominations[i] <= 10^6
  • All values in denominations are unique.

Examples

Input: amount = 230, denominations = [100, 50, 20, 10, 5, 1]
Output: 4

Two $100 bills, one $20 bill and one $10 bill make 230 with 4 bills. Three bills cannot reach 230: two $100 bills leave 30, which no single bill covers, and any three bills with at most one $100 among them sum to at most 200.

Input: amount = 3, denominations = [2]
Output: -1

Any number of $2 bills adds up to an even amount, so 3 cannot be formed.

Input: amount = 6, denominations = [4, 3, 1]
Output: 2

Two $3 bills make 6. Taking the largest bill first gives $4 + $1 + $1, which uses 3 bills, so the largest-first choice is not always the minimum.