LeetCode Problem Workspace

Maximum Product of Two Digits

Find the highest product obtainable from any two digits of a given positive integer using math and sorting techniques efficiently.

category

2

Topics

code_blocks

5

Code langs

hub

3

Related

Practice Focus

Easy · Math plus Sorting

bolt

Answer-first summary

Find the highest product obtainable from any two digits of a given positive integer using math and sorting techniques efficiently.

Interview AiBox logo

Ace coding interviews with Interview AiBox guidance for Math plus Sorting

Try AiBox Copilotarrow_forward

To solve this problem, extract all digits from the integer and sort them in descending order. The maximum product comes from multiplying the two largest digits. For repeated digits, you can use the same digit twice if it appears more than once in the number.

Problem Statement

You are given a positive integer n. Determine the maximum product obtainable from any two digits within n, using a direct math plus sorting approach.

Return the highest possible product of any two digits in n. If a digit occurs more than once, it can be paired with itself to maximize the product. The solution should handle numbers within the range 10 to 10^9 efficiently.

Examples

Example 1

Input: n = 31

Output: 3

Example 2

Input: n = 22

Output: 4

Example 3

Input: n = 124

Output: 8

Constraints

  • 10 <= n <= 109

Solution Approach

Brute Force Pair Comparison

Convert the integer to a list of digits and iterate through all pairs to compute their products. Track the maximum product encountered. This straightforward approach works well for small numbers and illustrates the direct math plus sorting pattern.

Sort and Multiply Top Two

Extract all digits, sort them in descending order, and multiply the first two digits. This leverages sorting to reduce comparisons and immediately identifies the two largest digits for the maximum product.

Optimized Single Pass

Iterate through the digits once while tracking the two largest digits seen so far. Multiply them at the end to get the maximum product. This avoids full sorting and reduces time complexity while still following the math plus sorting pattern.

Complexity Analysis

Metric Value
Time Depends on the final approach
Space Depends on the final approach

Time complexity is O(k log k) for sorting, where k is the number of digits, or O(k) for the single-pass optimization. Space complexity is O(k) to store digits or O(1) for the single-pass variant.

What Interviewers Usually Probe

  • Check if candidate handles repeated digits correctly.
  • Listen for sorting or digit extraction as a natural optimization.
  • See if the candidate considers single-pass vs full sort trade-offs.

Common Pitfalls or Variants

Common pitfalls

  • Forgetting that digits can repeat and pair with themselves.
  • Attempting full brute-force without considering sorting or optimization.
  • Mismanaging integer-to-digit conversion or edge cases like n having only two digits.

Follow-up variants

  • Compute maximum product of three digits instead of two.
  • Restrict digits to even numbers only for the product.
  • Return the digits forming the maximum product instead of the product itself.

FAQ

What is the maximum product of two digits pattern?

It involves extracting digits from n and either sorting or tracking the top two to compute their product efficiently.

Can the same digit be used twice in Maximum Product of Two Digits?

Yes, if a digit appears more than once, it can pair with itself to maximize the product.

What is the best approach for large n?

Use a single-pass approach to track the two largest digits, avoiding full sorting for better efficiency.

Are there edge cases I should watch for?

Yes, numbers with only two digits, repeated digits, or digits in ascending order require careful handling.

How does sorting help in this problem?

Sorting the digits in descending order lets you quickly identify the top two digits to compute the maximum product without checking all pairs.

terminal

Solution

Solution 1: Find the Largest and Second Largest Digits

We keep two variables, $a$ and $b$, to record the current largest and second‑largest digits, respectively. We iterate over every digit of $n$; if the current digit is larger than $a$, we assign $b$ the value of $a$ and then set $a$ to the current digit. Otherwise, if the current digit is larger than $b$, we set $b$ to the current digit. Finally, we return $a \times b$.

1
2
3
4
5
6
7
8
9
10
class Solution:
    def maxProduct(self, n: int) -> int:
        a = b = 0
        while n:
            n, x = divmod(n, 10)
            if a < x:
                a, b = x, a
            elif b < x:
                b = x
        return a * b
Maximum Product of Two Digits Solution: Math plus Sorting | LeetCode #3536 Easy