Maximum Product of Two Digits - Problem

Given a positive integer n, you need to find the maximum product of any two digits within that number.

๐ŸŽฏ Goal: Extract all individual digits from the number and find the pair that gives the highest product when multiplied together.

Important: You can use the same digit twice if it appears multiple times in the number. For example, in the number 323, you can multiply the digit 3 by itself since it appears twice.

Example: For n = 3726, the digits are [3, 7, 2, 6]. The maximum product would be 7 ร— 6 = 42.

Input & Output

example_1.py โ€” Basic Case
$ Input: n = 3726
โ€บ Output: 42
๐Ÿ’ก Note: The digits are [3, 7, 2, 6]. The largest two digits are 7 and 6, so 7 ร— 6 = 42 is the maximum product.
example_2.py โ€” Repeated Digits
$ Input: n = 9911
โ€บ Output: 81
๐Ÿ’ก Note: The digits are [9, 9, 1, 1]. The largest digit 9 appears twice, so 9 ร— 9 = 81 is the maximum product.
example_3.py โ€” Two Digit Number
$ Input: n = 23
โ€บ Output: 6
๐Ÿ’ก Note: The digits are [2, 3]. Since we only have two digits, the product is 2 ร— 3 = 6.

Constraints

  • 1 โ‰ค n โ‰ค 109
  • n is a positive integer
  • The number will have at least 2 digits

Visualization

Tap to expand
Maximum Product of Two DigitsExample: n = 8374Step 1: Extract Digits8374Step 2: Sort in Descending Order8743Two LargestStep 3: Calculate Maximum Product8ร—7=56Maximum!
Understanding the Visualization
1
Extract All Digits
Break down the number into individual digits
2
Identify Largest Two
Find the two highest valued digits
3
Calculate Product
Multiply these two digits for maximum result
Key Takeaway
๐ŸŽฏ Key Insight: The maximum product of any two digits always comes from multiplying the two largest digits in the number - no need to check all combinations!
Asked in
Google 25 Amazon 18 Apple 12 Microsoft 15
28.5K Views
Medium Frequency
~12 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen