#3536
Easy Algorithms Maximum product of two digits
Math Sorting
69.3% acceptance
Feb 25, 2026
67
3
You are given a positive integer n.
Return the maximum product of any two digits in n.
Note: You may use the same digit twice if it appears more than once in n.
Solution
Rust
Time O(n)
Space O(n)
impl Solution {
pub fn max_product(n: i32) -> i32 {
let mut digits: Vec<i32> = Vec::new();
let mut x = n;
while x > 0 {
digits.push(x % 10);
x /= 10;
}
digits.sort_unstable_by(|a, b| b.cmp(a));
digits[0] * digits[1]
}
}