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
3-Digit Even Number Vending MachineDigit Inventory: [1,2,3,2] โ†’ Coins Available1ร—12ร—23ร—1Other digits: ร—0 eachHundreds PlaceMust be: 1-9Available: 1,2,3โŒ No zeros allowed(prevents leading zero)Tens PlaceCan be: 0-9Available: 1,2,3โœ… Any available digit(check reuse limits)Units PlaceMust be: 0,2,4,6,8Available: 2 onlyโœ… Even digits only(makes number even)Valid Combinations Checkโœ… 122: uses 1(ร—1) + 2(ร—2) = validโœ… 132: uses 1(ร—1) + 3(ร—1) + 2(ร—1) = validโœ… 232: uses 2(ร—1) + 3(ร—1) + 2(ร—1) = validโŒ 222: needs 2(ร—3) but only have 2(ร—2)โŒ 123: ends in 3 (odd number)โŒ 032: starts with 0 (invalid)Result: [122, 132, 232, 322] (sorted)
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)

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Frequency array is fixed size 10, result size is bounded by 450 maximum valid numbers

n
2n
โœ“ 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
Asked in
Google 12 Amazon 8 Apple 6 Microsoft 4
26.8K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen