Maximum Product of Two Digits - Problem

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.

Input & Output

Example 1 — Basic Case
$ Input: n = 253
Output: 25
💡 Note: The digits are 2, 5, and 3. The maximum product is 5 × 5 = 25 (using digit 5 twice)
Example 2 — Single Digit Repeated
$ Input: n = 99
Output: 81
💡 Note: The digits are 9 and 9. The maximum product is 9 × 9 = 81
Example 3 — Different Digits
$ Input: n = 1234
Output: 16
💡 Note: The digits are 1, 2, 3, and 4. The maximum product is 4 × 4 = 16

Constraints

  • 10 ≤ n ≤ 109
  • n is a positive integer

Visualization

Tap to expand
Maximum Product of Two Digits Find Two Largest Digits Approach INPUT Given number n: 2 index 0 5 index 1 3 index 2 Input Value: n = 253 Digits: [2, 5, 3] Goal: Find maximum product of any two digits ALGORITHM STEPS 1 Extract Digits 253 % 10 = 3, 25 % 10 = 5... 2 Track Two Largest max1 = 0, max2 = 0 3 Update Max Values Compare each digit 4 Return Product max1 * max2 Iteration Trace: Digit max1 max2 3 3 0 5 5 3 2 5 3 FINAL RESULT Two Largest Digits Found: 5 max1 3 max2 Product Calculation: 5 x 3 = 15 OUTPUT 25 OK - Maximum Product Key Insight: Instead of checking all pairs O(n^2), track only the two largest digits in a single pass O(n). The maximum product is always the product of the two largest digits. Time: O(d) where d = number of digits. TutorialsPoint - Maximum Product of Two Digits | Optimized: Find Two Largest Digits
Asked in
Google 25 Amazon 15 Facebook 12
25.0K Views
Medium Frequency
~10 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