Given an integer array digits
where each element is a digit from 0 to 9, you need to find all the unique 3-digit even numbers that can be formed using exactly three of the given digits in digits
. Return the numbers in ascending order as a sorted list.
Here is the implementation based on the strategy:
from itertools import permutations
def findEvenNumbers(digits):
# Convert permutations of digits into potential 3-digit numbers
unique_numbers = set()
for perm in permutations(digits, 3):
# Check if last digit is even
if perm[2] % 2 == 0:
number = perm[0] * 100 + perm[1] * 10 + perm[2]
# Ensure it is a 3-digit number (first digit must not be zero)
if perm[0] != 0:
unique_numbers.add(number)
# Sort the unique numbers and return them as a list
return sorted(unique_numbers)
# Example usage
digits = [2, 1, 3, 0]
print(findEvenNumbers(digits)) # Output: [102, 120, 130, 132, 210, 230, 302, 310, 312, 320]
Now, this function findEvenNumbers
will generate and return all unique 3-digit even numbers that can be formed from the given digits, in ascending order.
Got blindsided by a question you didn’t expect?
Spend too much time studying?
Or simply don’t have the time to go over all 3000 questions?