Unique 3-Digit Even Numbers - Problem
Problem: You have a collection of digits, and you need to find how many distinct 3-digit even numbers you can form using them.

Rules:
• Each digit can only be used once per number
• No leading zeros allowed (numbers must be 100-999)
• The number must be even (last digit must be 0, 2, 4, 6, or 8)

Example: Given [2, 1, 3, 0], you can form: 102, 120, 130, 132, 210, 230, 302, 310, 312, 320 → 10 unique even numbers

Goal: Count all possible distinct 3-digit even numbers that can be formed.

Input & Output

example_1.py — Basic Case
$ Input: [2,1,3,0]
Output: 10
💡 Note: Possible even numbers: 102, 120, 130, 132, 210, 230, 302, 310, 312, 320. All digits 1-9 can be first digit (no leading zeros), any digit can be middle, and only even digits (0,2) can be last.
example_2.py — Duplicate Digits
$ Input: [2,2,8,8,2]
Output: 1
💡 Note: With three 2s and two 8s, we can form: 228, 282, 822, 288, 828, 882. However, only 228, 282, 822, 288, 828, 882 are valid... Wait, let me recalculate: we need 3 different positions. Possible: 228, 282, 822, 288, 828, 882. Actually the answer should be 6, but with uniqueness constraint, it's still 6 unique numbers.
example_3.py — Edge Case with Zero
$ Input: [0,2,0,0]
Output: 0
💡 Note: No valid 3-digit even numbers can be formed because we only have 0 and 2, but we need a non-zero first digit. Since we only have one 2, we cannot form any valid 3-digit number.

Visualization

Tap to expand
Building 3-Digit Even Numbers: License Plate FactoryAvailable Digit Stamps: [2,1,3,0]2130Step 1: Choose First Digit (1-9 only)2130Step 2: Choose Second Digit (any available)All remaining digits 0,1,2,3 are valid for middle positionStep 3: Choose Third Digit (even only)0✓ even2✓ even1,3❌ oddExample: Building 3123First1Second2Third✓ Valid: 3≠0, 1 available, 2 is evenFinal Valid Numbers:102, 120, 130, 132210, 230, 302, 310312, 320🎯 Key Insight: Position constraints eliminate invalid combinations early
Understanding the Visualization
1
Sort Your Stamps
Count how many of each digit (0-9) you have available
2
Choose First Digit
Pick from digits 1-9 only (no leading zeros allowed)
3
Choose Middle Digit
Pick from any remaining available digits (0-9)
4
Choose Last Digit
Pick from remaining even digits only (0,2,4,6,8)
5
Validate & Count
Ensure all chosen digits are available and add to result set
Key Takeaway
🎯 Key Insight: Instead of generating all permutations, we constrain our search by position requirements: first digit must be 1-9 (no leading zeros), last digit must be even (0,2,4,6,8), resulting in at most 9×10×5 = 450 combinations to check - a constant time solution!

Time & Space Complexity

Time Complexity
⏱️
O(1)

At most 10×10×5 = 500 combinations to check (digits 0-9, with constraints)

n
2n
Linear Growth
Space Complexity
O(1)

Fixed size frequency array for digits 0-9, result set bounded by max possible numbers

n
2n
Linear Space

Constraints

  • 1 ≤ digits.length ≤ 100
  • 0 ≤ digits[i] ≤ 9
  • Each element in digits represents a single digit
  • No leading zeros allowed in 3-digit numbers
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
26.7K 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