Finding 3-Digit Even Numbers (LeetCode 2094): Solution and Explanation

Easy
Array
Hash Table
Sorting
Enumeration

Given a bag of digits, list every distinct even three-digit number you can spell with them, in ascending order. Each digit in the bag can be used once.

Intuition

The obvious idea is to try every ordered triple of positions. With 100 digits that is close to a million triples, most of them duplicates that a set then has to remove, and the result still needs sorting.

Flip the question around. There are only 450 even numbers between 100 and 998. For each one, ask: can my bag of digits pay for it?

The bag only matters as counts. A number like 228 needs two 2s and one 8, so it can be built exactly when count[2] >= 2 and count[8] >= 1. Testing candidates from 100 upward also gives a sorted, duplicate-free answer with no extra work.

Approach: Digit counts plus enumeration

  1. Count how many times each digit 0 to 9 appears in digits.
  2. For every even num from 100 to 998, split it into hundreds a, tens b and ones c.
  3. Subtract one from count[a], count[b] and count[c]. If none went negative, the bag can pay for num, so append it.
  4. Add the three digits back and move on. Return the list.

Step-by-step example

digits = [2,1,3,0]
  1. The counts are one each of 0, 1, 2 and 3.
  2. 100 needs two 0s but the bag has one, so it is skipped. 102 uses 1, 0 and 2 once each, so it is kept.
  3. Continuing up, 120, 130 and 132 are kept, while numbers like 104 fail because there is no 4.
  4. The 200s give 210 and 230, the 300s give 302, 310, 312 and 320, and every number from 400 up needs a digit the bag lacks. Return the 10 numbers found.

Code: Python, Java, C++

1class Solution:
2    def findEvenNumbers(self, digits: List[int]) -> List[int]:
3        count = [0] * 10
4        for d in digits:
5            count[d] += 1
6        result = []
7        for num in range(100, 1000, 2):
8            a, b, c = num // 100, num // 10 % 10, num % 10
9            count[a] -= 1; count[b] -= 1; count[c] -= 1
10            if count[a] >= 0 and count[b] >= 0 and count[c] >= 0:
11                result.append(num)
12            count[a] += 1; count[b] += 1; count[c] += 1
13        return result
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Counting is O(n). The candidate loop always runs 450 times with constant work each, so it is O(1) with respect to n.

Space

O(1)

The count array has 10 slots. The output holds at most 450 numbers, a constant bound.

Other approaches

Triple loop with a set (time O(n^3), space O(1))

Try every ordered triple of distinct positions i, j, k, keep the valid numbers in a set, then sort. Simple but cubic in the input length.

Loop over digit values (time O(n), space O(1))

Loop a from 1 to 9, b from 0 to 9 and c over even digits, checking counts. This is the same idea as enumerating numbers, written as three nested loops.

Common mistakes

  • Testing each digit's count separately. 200 needs two 0s, so count[0] >= 1 is not enough; subtract all three digits first and then check.
  • Allowing a leading zero. Starting the loop at 100 rules it out.
  • Forgetting to restore the counts after each candidate, which corrupts every later check.
  • Returning results from a triple loop without removing duplicates or sorting.

Frequently asked questions

Why is enumerating numbers faster than arranging digits?

The answer space is fixed at 450 even numbers, while the number of digit triples grows with the cube of the input length.

Why is the output already sorted and unique?

Each candidate is tested once, in increasing order, so no number can appear twice and the list grows in ascending order.

What happens when the digits contain no even digit?

No candidate can be paid for, because every candidate ends in 0, 2, 4, 6 or 8. The answer is an empty list.