Most Expensive Item That Can Not Be Bought - Problem

The Frobenius Coin Problem

Alice is shopping at a magical market where every possible price exists as an item - there's an item priced at $1, $2, $3, and so on infinitely. She has two types of coins with prime denominations: primeOne and primeTwo, and she has an infinite supply of both.

Alice wants to find the most expensive item that she cannot possibly buy using any combination of her two coin types. This is a classic problem in number theory known as the Frobenius problem.

Example: If Alice has coins of denominations 3 and 5, she cannot buy items priced at 1, 2, 4, or 7, but she can buy everything from 8 onwards. So the answer would be 7.

Goal: Return the price of the most expensive item Alice cannot buy.

Input & Output

example_1.py โ€” Basic Case
$ Input: primeOne = 3, primeTwo = 5
โ€บ Output: 7
๐Ÿ’ก Note: With coins 3 and 5, we cannot make amounts 1, 2, 4, and 7. But we can make 8 (3+5), 9 (3+3+3), 10 (5+5), 11 (3+3+5), and all amounts โ‰ฅ 8. So 7 is the largest impossible amount.
example_2.py โ€” Larger Primes
$ Input: primeOne = 2, primeTwo = 7
โ€บ Output: 5
๐Ÿ’ก Note: With coins 2 and 7, we cannot make amounts 1, 3, and 5. We can make: 2, 4 (2+2), 6 (2+2+2), 7, 8 (2+2+2+2), 9 (2+7), etc. The largest impossible amount is 5.
example_3.py โ€” Small Primes
$ Input: primeOne = 2, primeTwo = 3
โ€บ Output: 1
๐Ÿ’ก Note: With coins 2 and 3, we can only make even amounts with 2s, and amounts 3,5,6,7,8,... with combinations. Only 1 cannot be made, so the answer is 1.

Constraints

  • 2 โ‰ค primeOne, primeTwo โ‰ค 103
  • primeOne and primeTwo are distinct prime numbers
  • The result will always be a positive integer

Visualization

Tap to expand
Frobenius Coin Problem: Coins 3 and 5Number Line: Which amounts can be made?123456789101112...Impossible AmountsCannot be made with coins 3 and 5:1, 2, 4, 7Frobenius Number = 7Possible AmountsExamples of coin combinations:8 = 3+5, 9 = 3+3+3, 10 = 5+511 = 3+3+5, 12 = 3+3+3+3All amounts โ‰ฅ 8 are possible!Mathematical FormulaFrobenius Number = a ร— b - a - bFor primes 3 and 5: 3 ร— 5 - 3 - 5 = 15 - 8 = 7
Understanding the Visualization
1
Identify Patterns
With coins 3 and 5, small amounts like 1, 2, 4 cannot be made
2
Find the Transition
Starting from amount 8, every amount can be made using coin combinations
3
Apply Formula
The mathematical formula aร—b - a - b gives us the largest impossible amount directly
Key Takeaway
๐ŸŽฏ Key Insight: For two coprime numbers (like distinct primes), the Frobenius formula aร—b - a - b gives us the largest amount that cannot be represented as their linear combination. This elegant mathematical result turns a complex combinatorial problem into a simple arithmetic calculation!
Asked in
Google 35 Microsoft 28 Amazon 22 Meta 18
52.3K Views
Medium Frequency
~15 min Avg. Time
1.8K 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