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:
n are [3, 1].3 * 1 = 3.Example 2:
Input: n = 22
Output: 4
Explanation:
n are [2, 2].2 * 2 = 4.Example 3:
Input: n = 124
Output: 8
Explanation:
n are [1, 2, 4].1 * 2 = 2, 1 * 4 = 4, 2 * 4 = 8.Constraints:
10 <= n <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The goal is to find the largest result we can get by multiplying any two digits in a number. A brute force approach means we'll try out every single possible pair of digits and see which pair gives us the biggest product.
Here's how the algorithm would work step-by-step:
def max_product_of_two_digits_brute_force(number):
number_string = str(number)
max_product = 0
# Iterate through all possible pairs of digits.
for first_digit_index in range(len(number_string)):
for second_digit_index in range(len(number_string)):
# Avoid multiplying a digit by itself.
if first_digit_index != second_digit_index:
first_digit = int(number_string[first_digit_index])
second_digit = int(number_string[second_digit_index])
product = first_digit * second_digit
# Update the maximum product if necessary.
if product > max_product:
max_product = product
return max_productThe goal is to find the two largest numbers within a list and then multiply them together. We want to avoid checking every possible pair, so we'll find the two largest numbers directly.
Here's how the algorithm would work step-by-step:
def max_product_of_two_digits(number_list):
largest_number = max(number_list)
# Store the largest number to calculate product
first_largest_number = largest_number
number_list.remove(largest_number)
# After removing the largest, remaining max is 2nd largest.
second_largest_number = max(number_list)
product = first_largest_number * second_largest_number
return product| Case | How to Handle |
|---|---|
| Null input | Return 0 or throw an IllegalArgumentException if the input is null. |
| Input array contains only one digit | Return 0 because we need at least two digits for a product. |
| Input array contains only zeros | Return 0 because the product of any two zeros is zero. |
| Input array contains negative numbers | Handle negative numbers correctly by considering the two most negative numbers which could produce the largest positive product. |
| Input array contains a mix of positive, negative, and zero numbers | Consider the largest two positive numbers and the two most negative numbers, and return the largest of those two products. |
| Input array with integer overflow potential in multiplication | Use a larger data type (e.g., long) to store the product, or check for potential overflow before multiplication. |
| Large input array for performance considerations | Use an efficient algorithm (e.g., sorting or two-pass linear scan) to find the two largest and two smallest numbers. |
| Duplicate largest or smallest numbers present | Algorithm must correctly identify unique indices or handle duplicates appropriately to avoid selecting the same number twice. |