Skip to main content

Maximum Product of Two Digits - Solution & Explanation

EasyMathSorting6 min readAsked at: Google
Practice this problem

Problem Statement

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.

 

Example 1:

Input: n = 31

Output: 3

Explanation:

  • The digits of n are [3, 1].
  • The possible products of any two digits are: 3 * 1 = 3.
  • The maximum product is 3.

Example 2:

Input: n = 22

Output: 4

Explanation:

  • The digits of n are [2, 2].
  • The possible products of any two digits are: 2 * 2 = 4.
  • The maximum product is 4.

Example 3:

Input: n = 124

Output: 8

Explanation:

  • The digits of n are [1, 2, 4].
  • The possible products of any two digits are: 1 * 2 = 2, 1 * 4 = 4, 2 * 4 = 8.
  • The maximum product is 8.

 

Constraints:

  • 10 <= n <= 109

Approach Overview

Problem Overview: You receive an integer and need the maximum product that can be formed using any two of its digits. The task reduces to identifying the two largest digits and multiplying them.

Approach 1: Brute Force Pair Comparison (O(d²) time, O(1) space)

Extract all digits from the number, typically by repeatedly applying n % 10 and n // 10. Store them in a list and compare every possible pair of digits. For each pair, compute the product and track the maximum value found. This approach is straightforward but inefficient because it performs nested iteration across the digits. With at most 10 digits in a typical integer it still works, but interviewers expect a more optimal scan.

Approach 2: Sort Digits (O(d log d) time, O(d) space)

Extract all digits and place them in an array, then sort the array in descending order using a standard sorting algorithm. The maximum product must come from the two largest digits, which will appear at the start of the sorted list. Multiply the first two elements and return the result. Sorting simplifies the logic and works well when you already plan to process digits collectively. This approach relies on basic sorting techniques and simple math operations.

Approach 3: Track Largest and Second Largest Digits (O(d) time, O(1) space)

Scan the digits once while maintaining two variables: largest and secondLargest. For each extracted digit, update these values using simple comparisons. If the current digit exceeds largest, shift the old largest into secondLargest. If it falls between them, update only secondLargest. After processing all digits, multiply the two stored values. This single-pass strategy avoids extra memory and sorting overhead, making it the most efficient solution using only constant space and basic math operations.

Recommended for interviews: Interviewers usually expect the single-pass approach that tracks the largest and second-largest digits. Starting with the brute force idea demonstrates understanding of the problem, but recognizing that only the top two digits matter shows stronger algorithmic thinking. The O(d) scan with constant space is both clean and optimal.

Solution

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.

The time complexity is O(log n), where n is the input number, and the space complexity is O(1).

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force Pair ComparisonO(d²)O(1)Good for demonstrating the basic idea or when digit count is extremely small
Sort DigitsO(d log d)O(d)Simple implementation when digits are already being stored or processed in arrays
Track Largest and Second LargestO(d)O(1)Best approach for interviews and production due to single pass and constant space

Video Solution

3536. Maximum Product of Two Digits (Leetcode Easy)Programming Live with Larry462 views views

Watch 2 more video solutions →

Frequently Asked Questions

Is Maximum Product of Two Digits easy or hard?
Maximum Product of Two Digits is categorized as an Easy problem. The main idea is recognizing that the maximum product must come from the two largest digits, which can be found with a simple linear scan.
Maximum Product of Two Digits Python/Java solution
In both Python and Java, extract digits using modulo (n % 10) and integer division. Maintain two variables representing the largest and second-largest digits. After iterating through all digits, return their product.
How to solve Maximum Product of Two Digits in O(n)?
Treat n as the number of digits. Extract digits using modulo and division operations, and maintain two variables for the largest and second-largest digits. Update them during a single pass through the digits, then multiply them to get the maximum product.
What is the best approach for Maximum Product of Two Digits?
The best approach is scanning the digits once while tracking the largest and second-largest digits. Each digit is processed using simple comparisons, and the final answer is their product. This method runs in O(d) time and uses O(1) extra space.
Is Maximum Product of Two Digits asked at Google/Amazon/Meta?
This style of digit-processing problem commonly appears in screening rounds and coding assessments at large tech companies. Variations involving extracting digits, tracking extremes, or computing digit-based metrics are frequently used to test basic algorithmic reasoning.
What data structure is used in Maximum Product of Two Digits?
The optimal solution does not require a complex data structure. It relies on simple integer variables to track the two largest digits while iterating through the number. Some alternative solutions temporarily store digits in an array for sorting.
What is the time complexity of Maximum Product of Two Digits?
The optimal solution runs in O(d) time, where d is the number of digits in the integer. Each digit is processed exactly once while maintaining the two largest values. Space complexity remains O(1) because only two variables are stored.

Ready to solve this problem?

Practice Maximum Product of Two Digits with our built-in code editor and test cases.

Practice on FleetCode