Problem Description
Given two distinct prime numbers primeOne and primeTwo, determine the most expensive item (i.e., the highest integer value) that cannot be purchased using any combination of coins with denominations primeOne and primeTwo. Since there is an infinite supply of items and coins, we are essentially seeking the largest integer that cannot be expressed as a non-negative linear combination of the two primes.
Key Insights
- The coins are prime numbers and distinct, so they are guaranteed to be coprime.
- This problem is a classic instance of the Frobenius Coin Problem (also known as the Chicken McNugget Theorem), which provides a formula for the largest integer that cannot be expressed as a combination of two coprime numbers.
- The formula is: (primeOne * primeTwo) - primeOne - primeTwo.
- Since the result is derived from a mathematical formula, the solution is computed in constant time.
Space and Time Complexity
Time Complexity: O(1)
Space Complexity: O(1)
Solution
The problem can be solved by recognizing it as a version of the Frobenius Coin Problem. For two coprime coin denominations, the largest value that cannot be made is given by (primeOne * primeTwo) - primeOne - primeTwo.
This solution uses simple arithmetic (multiplication and subtraction), and no additional data structures are necessary. The mathematical insight directly yields the answer without the need for iterative or recursive approaches.