You are given an integer array digits, where each element is a digit. The array may contain duplicates.
You need to find all the unique integers that follow the given requirements:
digits in any arbitrary order.For example, if the given digits were [1, 2, 3], integers 132 and 312 follow the requirements.
Return a sorted array of the unique integers.
Example 1:
Input: digits = [2,1,3,0] Output: [102,120,130,132,210,230,302,310,312,320] Explanation: All the possible integers that follow the requirements are in the output array. Notice that there are no odd integers or integers with leading zeros.
Example 2:
Input: digits = [2,2,8,8,2] Output: [222,228,282,288,822,828,882] Explanation: The same digit can be used as many times as it appears in digits. In this example, the digit 8 is used twice each time in 288, 828, and 882.
Example 3:
Input: digits = [3,7,5] Output: [] Explanation: No even integers can be formed using the given digits.
Constraints:
3 <= digits.length <= 1000 <= digits[i] <= 9When 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 to this problem means trying every possible combination of the given digits to form 3-digit numbers. We'll check each number to see if it meets the required conditions: being a 3-digit number and being even.
Here's how the algorithm would work step-by-step:
def find_3_digit_even_numbers(digits):
resulting_numbers = set()
# Iterate through all possible permutations of digits.
for first_index in range(len(digits)):
for second_index in range(len(digits)):
# Ensure that we don't reuse the same digit.
if first_index == second_index:
continue
for third_index in range(len(digits)):
if third_index == first_index or third_index == second_index:
continue
number = digits[first_index] * 100 + digits[second_index] * 10 + digits[third_index]
# Numbers must be three digits and even
if 100 <= number <= 998:
if number % 2 == 0:
resulting_numbers.add(number)
sorted_numbers = sorted(list(resulting_numbers))
return sorted_numbersThe most efficient way to find all the possible 3-digit even numbers is to count how many times each digit appears. Then, we build the even numbers from the digits we have, making sure they're in ascending order and removing any duplicates.
Here's how the algorithm would work step-by-step:
def find_3_digit_even_numbers(digits):
digit_counts = {}
for digit in digits:
digit_counts[digit] = digit_counts.get(digit, 0) + 1
results = []
# Iterate through possible even ending digits.
for last_digit in [0, 2, 4, 6, 8]:
if last_digit not in digit_counts or digit_counts[last_digit] == 0:
continue
digit_counts[last_digit] -= 1
# Iterate through possible first digits.
for first_digit in range(1, 10):
if first_digit not in digit_counts or digit_counts[first_digit] == 0:
continue
digit_counts[first_digit] -= 1
# Iterate through possible second digits.
for second_digit in range(0, 10):
if second_digit not in digit_counts or digit_counts[second_digit] == 0:
continue
# Construct the 3-digit number.
number = first_digit * 100 + second_digit * 10 + last_digit
# Add the number to the results list.
results.append(number)
digit_counts[first_digit] += 1
digit_counts[last_digit] += 1
# Remove duplicate numbers and then sort the results.
unique_numbers = sorted(list(set(results)))
return unique_numbers| Case | How to Handle |
|---|---|
| Empty input array | Return an empty list, as no 3-digit even numbers can be formed. |
| Array with length less than 3 | Return an empty list, since at least three digits are required. |
| Input array contains only zeros | Return a list containing only [0, 0, 0] once, representing '000' if the array has at least 3 zeros, otherwise return empty list. |
| No combination of digits forms a 3-digit even number | Return an empty list to indicate no valid numbers were found. |
| Input array contains negative numbers | Ignore negative numbers, as the problem specifies forming 3-digit numbers using digits. |
| Input array contains only odd numbers | Return an empty list since an even number requires at least one even digit. |
| The input array contains very large numbers | Ignore numbers greater than 9, as we are only looking for digits to form 3-digit numbers. |
| Input array contains duplicate 3-digit even numbers | The solution should correctly generate all unique 3-digit even numbers; use a set to avoid duplicates in the result. |