Count the ways to buy pens and pencils within a budget

Quick Overview

Count how many combinations of pens and pencils fit within a fixed budget when each item has its own price and you may spend less than the full amount. Tests turning a two-variable counting problem into one efficient loop, choosing loop bounds, and handling answers too large for 32-bit integers.

Count the ways to buy pens and pencils within a budget

Company: Reddit

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You have `total` units of money. A pen costs `cost1` and a pencil costs `cost2`. You can spend part or all of the money, and you can buy any number of each item, including none. Return the number of distinct ways you can buy some number of pens and pencils. ### Function Signature ```python def ways_to_buy_pens_pencils(total: int, cost1: int, cost2: int) -> int: ``` ### Rules - A way is a pair (number of pens, number of pencils) whose combined price is at most `total`. - Two ways are different when they differ in the number of pens or in the number of pencils. - Buying nothing at all (0 pens and 0 pencils) counts as one way. ### Constraints - `1 <= total, cost1, cost2 <= 10^6` ### Examples ```text Input: total = 20, cost1 = 10, cost2 = 5 Output: 9 ``` With 0 pens you can buy 0 to 4 pencils (5 options), with 1 pen you can buy 0 to 2 pencils (3 options), and with 2 pens the money is gone, so only 0 pencils (1 option). That gives 5 + 3 + 1 = 9. ```text Input: total = 5, cost1 = 10, cost2 = 10 Output: 1 ``` Both items cost more than the budget, so the only way is to buy 0 pens and 0 pencils. ```text Input: total = 7, cost1 = 3, cost2 = 2 Output: 8 ``` The valid pairs are (0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2) and (2, 0). Two pens and one pencil would cost 8, which is over the budget.

Overview: Count how many combinations of pens and pencils fit within a fixed budget when each item has its own price and you may spend less than the full amount. Tests turning a two-variable counting problem into one efficient loop, choosing loop bounds, and handling answers too large for 32-bit integers.

Reddit logo
Reddit
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You have total units of money. A pen costs cost1 and a pencil costs cost2. You can spend part or all of the money, and you can buy any number of each item, including none.

Return the number of distinct ways you can buy some number of pens and pencils.

Function Signature

def ways_to_buy_pens_pencils(total: int, cost1: int, cost2: int) -> int:

Rules

  • A way is a pair (number of pens, number of pencils) whose combined price is at most total .
  • Two ways are different when they differ in the number of pens or in the number of pencils.
  • Buying nothing at all (0 pens and 0 pencils) counts as one way.

Constraints

  • 1 <= total, cost1, cost2 <= 10^6

Examples

Input: total = 20, cost1 = 10, cost2 = 5
Output: 9

With 0 pens you can buy 0 to 4 pencils (5 options), with 1 pen you can buy 0 to 2 pencils (3 options), and with 2 pens the money is gone, so only 0 pencils (1 option). That gives 5 + 3 + 1 = 9.

Input: total = 5, cost1 = 10, cost2 = 10
Output: 1

Both items cost more than the budget, so the only way is to buy 0 pens and 0 pencils.

Input: total = 7, cost1 = 3, cost2 = 2
Output: 8

The valid pairs are (0, 0), (0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2) and (2, 0). Two pens and one pencil would cost 8, which is over the budget.