Skip to main content
Back to problems
#2979
Medium Algorithms

Most expensive item that can not be bought

Math Dynamic Programming Number Theory
80.4% acceptance
Mar 31, 2026
23
28
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.

Solution

Rust
Time O(1)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn most_expensive_item(prime_one: i32, prime_two: i32) -> i32 {
    prime_one * prime_two - prime_one - prime_two
  }
}