You are given an array of digits called digits
. Your task is to determine the number of distinct three-digit even numbers that can be formed using these digits.
Note: Each copy of a digit can only be used once per number, and there may not be leading zeros.
Example 1:
Input: digits = [1,2,3,4]
Output: 12
Explanation: The 12 distinct 3-digit even numbers that can be formed are 124, 132, 134, 142, 214, 234, 312, 314, 324, 342, 412, and 432. Note that 222 cannot be formed because there is only 1 copy of the digit 2.
Example 2:
Input: digits = [0,2,2]
Output: 2
Explanation: The only 3-digit even numbers that can be formed are 202 and 220. Note that the digit 2 can be used twice because it appears twice in the array.
Example 3:
Input: digits = [6,6,6]
Output: 1
Explanation: Only 666 can be formed.
Example 4:
Input: digits = [1,3,5]
Output: 0
Explanation: No even 3-digit numbers can be formed.
Constraints:
3 <= digits.length <= 10
0 <= digits[i] <= 9
When 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 brute force approach here is like trying every possible 3-digit combination using the given digits, and then checking if the number meets the requirements: being even and having unique digits. We will generate every number possible and filter out the ones that don't fit.
Here's how the algorithm would work step-by-step:
def find_unique_even_numbers(digits):
unique_even_numbers = set()
for hundreds_digit in digits:
for tens_digit in digits:
for ones_digit in digits:
three_digit_number = hundreds_digit * 100 + tens_digit * 10 + ones_digit
# Ensure each digit is unique
if (hundreds_digit != tens_digit and
hundreds_digit != ones_digit and
tens_digit != ones_digit):
# Check if the number is even
if three_digit_number % 2 == 0 and three_digit_number >= 100:
unique_even_numbers.add(three_digit_number)
# Convert the set to a list and sort it
sorted_unique_even_numbers = sorted(list(unique_even_numbers))
return sorted_unique_even_numbers
The best way to solve this is to carefully construct the possible 3-digit even numbers from the given digits. We want to avoid creating duplicates and make sure each number we create is actually even. We only need to construct the numbers that are even and meet all other rules.
Here's how the algorithm would work step-by-step:
def find_unique_even_numbers(digits):
available_digits = digits
unique_even_numbers = set()
# Iterate through possible even digits for the last position
for last_digit in sorted(list(set([digit for digit in available_digits if digit % 2 == 0]))):
remaining_digits = available_digits[:]
remaining_digits.remove(last_digit)
# Iterate through possible digits for the first position
for first_digit in sorted(list(set([digit for digit in remaining_digits if digit != 0]))):
remaining_digits_second = remaining_digits[:]
remaining_digits_second.remove(first_digit)
# Iterate through possible digits for the second position
for second_digit in sorted(list(set(remaining_digits_second))):
number = first_digit * 100 + second_digit * 10 + last_digit
# Ensure number is 3 digits, even, and unique
if 100 <= number <= 999 and number % 2 == 0:
unique_even_numbers.add(number)
# Sort the unique numbers
sorted_unique_even_numbers = sorted(list(unique_even_numbers))
return sorted_unique_even_numbers
Case | How to Handle |
---|---|
Empty input array | Return an empty list since no 3-digit numbers can be formed. |
Input array size less than 3 | Return an empty list since we need at least three digits to form a 3-digit number. |
Input array contains only odd digits | Return an empty list because no even 3-digit number can be formed. |
Input array contains no digits to form a hundred (e.g. no non-zero digits) | The algorithm should correctly skip combinations where the first digit is zero. |
Input array with all the same digits (e.g., [2, 2, 2, 2]) | The algorithm should generate a result (e.g. [222]) if valid and possible, otherwise return an empty list if not. |
Input array containing zero and only two other distinct digits | Check for valid even combinations. e.g. [0, 1, 2], should yield [120, 210]. |
Duplicate valid combinations are possible, must return unique result | Store the result in a set to ensure uniqueness, and convert to list at the end. |
Very large input array with a limited number of unique digits | Algorithm must still execute efficiently; using a set to ensure uniqueness is crucial |