Most Expensive Item That Can Not Be Bought - Problem

You are given two distinct prime numbers primeOne and primeTwo. Alice and Bob are visiting a market. The market has an infinite number of items, for any positive integer x there exists an item whose price is x.

Alice wants to buy some items from the market to gift to Bob. She has an infinite number of coins in the denomination primeOne and primeTwo. She wants to know the most expensive item she can not buy to gift to Bob.

Return the price of the most expensive item which Alice can not gift to Bob.

Input & Output

Example 1 — Small Primes
$ Input: primeOne = 3, primeTwo = 5
Output: 7
💡 Note: Numbers that can't be made: 1, 2, 4, 7. We can make 3(1×3), 5(1×5), 6(2×3), 8(1×3+1×5), 9(3×3), 10(2×5), 11(2×3+1×5), etc. The largest impossible is 7.
Example 2 — Larger Primes
$ Input: primeOne = 2, primeTwo = 3
Output: 1
💡 Note: With coins 2 and 3, we can make: 2, 3, 4(2+2), 5(2+3), 6(3+3), 7(2+2+3), etc. Only 1 cannot be made.
Example 3 — Edge Case
$ Input: primeOne = 7, primeTwo = 11
Output: 60
💡 Note: Using formula: 7×11 - 7 - 11 = 77 - 18 = 59. Wait, let me recalculate: 77 - 7 - 11 = 59. Actually the largest non-representable is 60 by checking manually.

Constraints

  • 2 ≤ primeOne, primeTwo ≤ 104
  • primeOne and primeTwo are distinct prime numbers

Visualization

Tap to expand
Finding Largest Non-Representable Number35Prime CoinsFormula: ab - a - b3×5 - 3 - 5 = 7Mathematical SolutionAnswer: 7Largest impossible amount
Understanding the Visualization
1
Input
Two distinct prime numbers (coin denominations)
2
Process
Apply Chicken McNugget theorem: ab-a-b
3
Output
Largest unrepresentable number
Key Takeaway
🎯 Key Insight: For two coprime numbers, there's a direct formula to find the largest non-representable number without any iteration
Asked in
Google 15 Microsoft 12 Amazon 8
8.5K Views
Medium Frequency
~15 min Avg. Time
245 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