Unique 3-Digit Even Numbers - Problem

You are given an array of digits called digits. Your task is to determine the number of distinct three-digit even numbers that can be formed using these digits.

Note: Each copy of a digit can only be used once per number, and there may not be leading zeros.

Input & Output

Example 1 — Basic Case
$ Input: digits = [2,1,3,0]
Output: 2
💡 Note: Can form: 210 (2×100+1×10+0), 230 (2×100+3×10+0). Both are even 3-digit numbers with no leading zeros.
Example 2 — Multiple Even Digits
$ Input: digits = [2,2,8,8,2]
Output: 3
💡 Note: Can form: 228, 282, 822. All are distinct 3-digit even numbers using available digits.
Example 3 — No Valid Numbers
$ Input: digits = [3,7,5]
Output: 0
💡 Note: All digits are odd, so no 3-digit even numbers can be formed.

Constraints

  • 1 ≤ digits.length ≤ 100
  • 0 ≤ digits[i] ≤ 9

Visualization

Tap to expand
Unique 3-Digit Even Numbers INPUT digits array: 2 [0] 1 [1] 3 [2] 0 [3] Even digits (for last position): 2 0 Constraints: - No leading zeros - Each digit used once - 3-digit even numbers - Range: 100-998 ALGORITHM STEPS 1 Use Set for uniqueness Store valid numbers in Set 2 Generate permutations Pick 3 different positions 3 Validate each number Check even + no leading 0 4 Count unique results Return Set size Valid Numbers Found: 120 130 Invalid: 012 (leading 0), 123 (odd) Invalid: 213, 231, 321 (all odd) Invalid: 012, 032 (leading zeros) FINAL RESULT Unique 3-Digit Even Numbers: 1 2 0 120 = even (ends in 0) 1 3 0 130 = even (ends in 0) Output: 2 OK - 2 unique numbers (120 and 130) Key Insight: For a 3-digit even number: the first digit cannot be 0 (no leading zeros), and the last digit must be even (0, 2, 4, 6, 8). Using a Set automatically handles duplicates when same digits appear multiple times in input. Time Complexity: O(n^3) for generating all 3-digit permutations. Space: O(n^3) for storing unique numbers. TutorialsPoint - Unique 3-Digit Even Numbers | Optimal Solution
Asked in
Google 25 Amazon 18 Microsoft 15 Meta 12
28.5K Views
Medium Frequency
~15 min Avg. Time
856 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