Number of Ways to Buy Pens and Pencils - Problem

You're planning to stock up on writing supplies for the upcoming school year! πŸ“

Given your budget of total dollars, you can purchase pens at cost1 dollars each and pencils at cost2 dollars each. You want to find out how many different ways you can spend your money.

Here's the twist: you can buy any quantity of each item (including zero), and you don't have to spend all your money. Each different combination of pens and pencils counts as a distinct way.

Example: If you have $20, pens cost $1, and pencils cost $2, you could buy:

  • 0 pens, 0 pencils (spend $0)
  • 1 pen, 0 pencils (spend $1)
  • 0 pens, 1 pencil (spend $2)
  • And many more combinations...

Goal: Return the total number of distinct ways to buy some combination of pens and pencils within your budget.

Input & Output

example_1.py β€” Basic case
$ Input: total = 20, cost1 = 1, cost2 = 1
β€Ί Output: 231
πŸ’‘ Note: With $20 and both items costing $1 each, we can buy any combination from (0,0) to (20,0), (19,1), (18,2), ..., (0,20). This forms a triangular counting pattern: 21 + 20 + 19 + ... + 1 = 231 ways.
example_2.py β€” Different costs
$ Input: total = 5, cost1 = 10, cost2 = 10
β€Ί Output: 1
πŸ’‘ Note: Since both pens and pencils cost $10 each but we only have $5, we can't afford any items. The only way is to buy 0 pens and 0 pencils, giving us 1 way total.
example_3.py β€” Asymmetric costs
$ Input: total = 20, cost1 = 1, cost2 = 2
β€Ί Output: 121
πŸ’‘ Note: We can buy 0-20 pens ($1 each). For each pen count: 0 pens allows 0-10 pencils (11 ways), 1 pen allows 0-9 pencils (10 ways), etc. Total: 11+10+9+8+7+6+5+4+3+2+1 = 66... Wait, let me recalculate: 11+10+10+9+9+8+8+7+7+6+6+5+5+4+4+3+3+2+2+1+1 = 121 ways.

Visualization

Tap to expand
Pen & Pencil Shopping GridNumber of Pens ($1 each)Number of Pencils ($2 each)Budget = $20Pens: $1 eachPencils: $2 each● Valid combinationsEach column shows penciloptions for fixed pen count1 pen β†’ 10 pencil options
Understanding the Visualization
1
Set up the problem space
Create a grid where x-axis = pens, y-axis = pencils. Each point (x,y) costs xΓ—cost1 + yΓ—cost2
2
Identify valid region
All points where total cost ≀ budget form a triangular/curved region
3
Count efficiently
Instead of checking each point, count points in each column (fixed pen count) mathematically
Key Takeaway
🎯 Key Insight: Instead of checking every (pen, pencil) combination, fix the pen count and mathematically calculate how many pencil options fit the remaining budget. This transforms a 2D enumeration problem into efficient 1D calculation!

Time & Space Complexity

Time Complexity
⏱️
O(total/cost1)

We only iterate through possible pen counts once, calculating pencil combinations mathematically

n
2n
βœ“ Linear Growth
Space Complexity
O(1)

Only using a few variables for calculations

n
2n
βœ“ Linear Space

Constraints

  • 0 ≀ total ≀ 106
  • 1 ≀ cost1, cost2 ≀ 106
  • The answer will fit in a 64-bit signed integer
Asked in
Amazon 15 Google 12 Microsoft 8 Meta 6
24.6K Views
Medium Frequency
~12 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
πŸ’‘ Explanation
AI Ready
πŸ’‘ Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen