3536. Maximum Product of Two Digits
Difficulty: Easy
Topics: Mid Level, Math, Sorting, Weekly Contest 448
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
nare[3, 1]. - The possible products of any two digits are:
3 * 1 = 3. - The maximum product is 3.
- The digits of
Example 2:
- Input: n = 22
- Output: 4
-
Explanation:
- The digits of
nare[2, 2]. - The possible products of any two digits are:
2 * 2 = 4. - The maximum product is 4.
- The digits of
Example 3:
- Input: n = 124
- Output: 8
-
Explanation:
- The digits of
nare[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.
- The digits of
Example 4:
- Input: n = 99
- Output: 81
Example 5:
- Input: n = 101
- Output: 1
Example 6:
- Input: n = 1000000000
- Output: 0
Example 7:
- Input: n = 987
- Output: 72
Example 8:
- Input: n = 555
- Output: 25
Constraints:
10 <= n <= 10⁹
Hint:
- Use brute force
Solution:
We convert the integer to a string, split it into an array of digits, and then use a nested loop to compute the product of every pair of digits (ensuring we don't pair a digit with itself unless it appears twice). We track and return the maximum product found.
Approach
- Convert the integer
nto a string and then to an array of digits. - Initialize a variable
maxProductto 0. - Use two nested loops:
- Outer loop
ifrom 0 tolen(digits)-1. - Inner loop
jfromi+1tolen(digits)-1(to avoid duplicate pairs).
- Outer loop
- Compute
product = digits[i] * digits[j]. - Update
maxProductifproductis larger. - Return
maxProductafter loops.
Let's implement this solution in PHP: 3536. Maximum Product of Two Digits
<?php
/**
* @param Integer $n
* @return Integer
*/
function maxProduct(int $n): int
{
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo maxProduct(31) . "\n"; // Output: 3
echo maxProduct(22) . "\n"; // Output: 4
echo maxProduct(124) . "\n"; // Output: 8
echo maxProduct(99) . "\n"; // Output: 81
echo maxProduct(101) . "\n"; // Output: 1
echo maxProduct(1000000000) . "\n"; // Output: 0
echo maxProduct(987) . "\n"; // Output: 72
echo maxProduct(555) . "\n"; // Output: 25
?>
Explanation:
-
Digit extraction: Convert
nto string and split to get each digit as an integer. - Pair iteration: The nested loops consider every unordered pair of distinct indices, which covers all combinations of two digits (including duplicates if they exist at different positions).
- Product calculation: Multiply the two digits and compare with the current maximum.
-
Edge cases: The constraints ensure
nhas at least two digits, so the loops will always run. - No need to sort: Brute force is sufficient since at most 10 digits → at most 45 pairs.
Complexity Analysis
-
Time Complexity: O(d²), where
dis the number of digits inn. Sinced ≤ 10(becausen ≤ 10⁹), this is effectively constant time. - Space Complexity: O(d) for storing the digit array, which is also constant.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:
Top comments (0)