Finding 3-Digit Even Numbers (LeetCode 2094): Solution and Explanation
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
- Count how many times each digit 0 to 9 appears in
digits. - For every even
numfrom 100 to 998, split it into hundredsa, tensband onesc. - Subtract one from
count[a],count[b]andcount[c]. If none went negative, the bag can pay fornum, so append it. - Add the three digits back and move on. Return the list.
Step-by-step example
digits = [2,1,3,0]
- The counts are one each of 0, 1, 2 and 3.
- 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.
- Continuing up, 120, 130 and 132 are kept, while numbers like 104 fail because there is no 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 result1class Solution {
2 public int[] findEvenNumbers(int[] digits) {
3 int[] count = new int[10];
4 for (int d : digits) {
5 count[d]++;
6 }
7 List<Integer> result = new ArrayList<>();
8 for (int num = 100; num < 1000; num += 2) {
9 int a = num / 100, b = num / 10 % 10, c = num % 10;
10 count[a]--; count[b]--; count[c]--;
11 if (count[a] >= 0 && count[b] >= 0 && count[c] >= 0) {
12 result.add(num);
13 }
14 count[a]++; count[b]++; count[c]++;
15 }
16 int[] arr = new int[result.size()];
17 for (int i = 0; i < arr.length; i++) arr[i] = result.get(i);
18 return arr;
19 }
20}1class Solution {
2public:
3 vector<int> findEvenNumbers(vector<int>& digits) {
4 int count[10] = {0};
5 for (int d : digits) {
6 count[d]++;
7 }
8 vector<int> result;
9 for (int num = 100; num < 1000; num += 2) {
10 int a = num / 100, b = num / 10 % 10, c = num % 10;
11 count[a]--; count[b]--; count[c]--;
12 if (count[a] >= 0 && count[b] >= 0 && count[c] >= 0) {
13 result.push_back(num);
14 }
15 count[a]++; count[b]++; count[c]++;
16 }
17 return result;
18 }
19};Time and space complexity
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.
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] >= 1is 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.