Maximum Product of Two Digits (LeetCode 3536): Solution and Explanation
Given a positive integer n, return the largest product of two digits taken from two different positions of n.
Intuition
Digits are never negative, so a bigger factor never makes a product smaller. The best pair is simply the two largest digits.
That turns the problem into finding the top two values in a short list, which one pass with two variables can do.
A repeated digit counts as two separate candidates. If 7 appears twice, both seats can hold a 7 and the answer is 49.
Approach: Track the top two digits
- Set
a = 0(largest digit) andb = 0(second largest). - While
n > 0, read the last digitd = n % 10. - If
d > a, moveadown intoband putdina. - Otherwise, if
d > b, replacebwithd. - Drop the digit with
n //= 10, then returna * bafter the loop.
Step-by-step example
n = 31
- a = 0, b = 0.
- The last digit is 1. 1 > a = 0, so a becomes 1 and b becomes 0.
- n is now 3. The digit 3 > a = 1, so a becomes 3 and the old a slides down: b = 1.
- n is 0, the loop ends and we return 3 * 1 = 3.
Code: Python, Java, C++
1class Solution:
2 def maxProduct(self, n: int) -> int:
3 a = b = 0
4 while n > 0:
5 d = n % 10
6 if d > a:
7 a, b = d, a
8 elif d > b:
9 b = d
10 n //= 10
11 return a * b1class Solution {
2 public int maxProduct(int n) {
3 int a = 0, b = 0;
4 while (n > 0) {
5 int d = n % 10;
6 if (d > a) {
7 b = a;
8 a = d;
9 } else if (d > b) {
10 b = d;
11 }
12 n /= 10;
13 }
14 return a * b;
15 }
16}1class Solution {
2public:
3 int maxProduct(int n) {
4 int a = 0, b = 0;
5 while (n > 0) {
6 int d = n % 10;
7 if (d > a) {
8 b = a;
9 a = d;
10 } else if (d > b) {
11 b = d;
12 }
13 n /= 10;
14 }
15 return a * b;
16 }
17};Time and space complexity
O(log n)
The loop runs once per digit, and n has at most 10 digits.
O(1)
Only the two seats a and b and the current digit are stored.
Other approaches
Sort the digits (time O(d log d), space O(d))
Turn n into a list of digits, sort it, and multiply the last two. It is short and clear, but sorting is more work than one pass.
Digit counting (time O(d + 10), space O(1))
Count how many times each digit 0 to 9 appears, then walk from 9 down and take the first two available digits. Useful when the input is a long digit string.
Common mistakes
- Forgetting to move the old leader into
bwhen a new largest digit arrives, which loses a valid second factor. - Deduplicating the digits. A repeated digit is a second candidate, so
22must give 4, not 0. - Treating zeros specially. A zero simply never beats a seat, and if only one nonzero digit exists the answer is correctly 0.
Frequently asked questions
Why is the answer always the two largest digits?
Every digit is between 0 and 9, so making either factor larger can only keep the product the same or increase it.
Can the answer be 0?
Yes. For n = 10 or n = 1000000000 there is only one nonzero digit, so any pair includes a 0.
Is converting n to a string acceptable?
Yes. Looping over the characters works just as well. The % 10 version avoids the allocation and shows the standard digit peeling pattern.