The Number of Ways to Buy Pens and Pencils problem gives you a total budget and the costs of pens and pencils (cost1 and cost2). Count the number of ways to spend some or all of the budget on pens and pencils (you can buy 0 or more of each). This easy math problem uses integer division enumeration.
Reddit asks this to test simple enumeration within a mathematical budget constraint. The key insight: fix the number of pens from 0 to budget/cost1, then for each pen count, compute how many pencils can be bought with the remaining budget. The math and enumeration interview pattern is applied concisely.
Enumerate pen count + floor division for pencils. For pens = 0, 1, 2, ..., budget // cost1: remaining = budget - pens * cost1. Pencils = 0 to remaining // cost2. That's remaining // cost2 + 1 valid pencil counts (including 0). Sum all pencil counts across all pen counts.
budget=10, cost1=4, cost2=3.
remaining // cost2 + 1 valid counts (0 through max), not just remaining // cost2.budget // cost1 (inclusive).Two-variable budget problems enumerate one variable and compute the other with floor division. This O(budget/cost1) approach is efficient for reasonable inputs. The +1 in the pencil count formula accounts for buying 0 pencils. Practice similar "count ordered pairs (a,b) where cost1a + cost2b ≤ budget" — they follow the same enumeration pattern. The key skill is recognizing floor division as the tool for "how many fit within remaining budget."
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Minimum Cost to Set Cooking Time | Medium | Solve | |
| Minimum Moves to Capture The Queen | Medium | Solve | |
| Smallest Greater Multiple Made of Two Digits | Medium | Solve | |
| Sum of Number and Its Reverse | Medium | Solve | |
| Consecutive Numbers Sum | Hard | Solve |