This is a premium problem. We're working on making it available for free soon.
Use these hints if you're stuck. Try solving on your own first.
Write out a few cases. It can be seen that all items greater than <code>primeOne * primeTwo</code> can always be bought.
If we can buy items with cost <code>i</code>, we can also buy items with price <code>i + primeOne</code> and <code>i + primeTwo</code>.
Use dynamic programming.
There is an O(1) solution: Use the Chicken McNugget Theorem.
Solutions for this premium problem will be available for free soon.
Browse Free ProblemsWatch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Problems based on the Frobenius coin concept and number theory insights appear in technical interviews at large tech companies. While the exact problem may vary, interviewers often test candidates on recognizing mathematical shortcuts instead of brute-force methods.
Yes, you could simulate reachable sums using dynamic programming or a boolean array to track which totals can be formed. However, this approach is less efficient compared to the mathematical solution because it explores many combinations unnecessarily.
The optimal approach uses a number theory insight from the Frobenius coin problem. Since the two prices are coprime, the largest value that cannot be represented as a combination of the two can be computed using a direct mathematical relationship. This avoids brute-force enumeration of combinations.
The problem reduces to determining which integers can be expressed as linear combinations of two coprime numbers. Number theory proves that beyond a certain point, all values become representable. The largest non-representable value follows a known mathematical property.