Finding 3-Digit Even Numbers - Problem
You have a collection of digit tiles, where each tile contains a single digit (0-9). Your task is to create 3-digit even numbers using exactly three tiles from your collection.
Goal: Find all unique 3-digit even numbers that can be formed by selecting and arranging three digits from your collection.
Rules:
- Each number must be exactly 3 digits long
- No leading zeros allowed (must be between 100-999)
- The number must be even (last digit is 0, 2, 4, 6, or 8)
- You can use the same digit multiple times if it appears multiple times in your collection
Example: With digits [1, 2, 3], you can form 132 and 312 (both are 3-digit even numbers), but not 123 (odd) or 021 (leading zero).
Return the results in sorted ascending order with no duplicates.
Input & Output
example_1.py โ Basic Case
$
Input:
[1, 2, 3]
โบ
Output:
[132, 312]
๐ก Note:
From digits [1,2,3], we can form two 3-digit even numbers: 132 (ends with 2) and 312 (ends with 2). Numbers like 123 are invalid (odd), and 321 is invalid (odd).
example_2.py โ With Duplicates
$
Input:
[2, 1, 3, 0]
โบ
Output:
[102, 120, 130, 132, 210, 230, 302, 310, 312, 320]
๐ก Note:
With digits [2,1,3,0], we can form various even numbers. Note that 102 is valid (even, no leading zero), but 012 would be invalid (leading zero). The 0 can be used in tens or units position.
example_3.py โ Multiple Same Digits
$
Input:
[2, 2, 8, 8, 2]
โบ
Output:
[222, 228, 282, 288, 822, 828, 882]
๐ก Note:
With three 2's and two 8's, we can create numbers like 222 (uses all three 2's), 228 (uses two 2's and one 8), etc. All results must be even and have no leading zeros.
Visualization
Tap to expand
Understanding the Visualization
1
Inventory Check
Count how many of each digit coin (0-9) we have available
2
Choose Hundreds
Select a non-zero digit for the first position (1-9 only)
3
Choose Tens
Pick any available digit for the middle position (0-9)
4
Choose Units
Select an even digit for the last position (0,2,4,6,8)
5
Validate Coins
Ensure we have enough digit coins for repeated digits
Key Takeaway
๐ฏ Key Insight: Instead of generating all permutations, we systematically build valid numbers by enforcing constraints at each position, making the algorithm much more efficient with O(1) time complexity.
Time & Space Complexity
Time Complexity
O(1)
Since we iterate through at most 9ร10ร5 = 450 combinations (constant), this is O(1)
โ Linear Growth
Space Complexity
O(1)
Frequency array is fixed size 10, result size is bounded by 450 maximum valid numbers
โ Linear Space
Constraints
-
3 โค digits.length โค 100 -
0 โค digits[i] โค 9 - Each element in digits is a single digit (0-9)
- The array may contain duplicate digits
- Return results in sorted ascending order
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code