[LeetCode] Finding 3-Digit Even Numbers

2094. Finding 3-Digit Even Numbers

You are given an integer array digits, where each element is a digit. The array may contain duplicates.

You need to find all the unique integers that follow the given requirements:

  • The integer consists of the concatenation of three elements from digits in any arbitrary order.
  • The integer does not have leading zeros.
  • The integer is even.

For example, if the given digits were [1, 2, 3], integers 132 and 312 follow the requirements.

Return a sorted array of the unique integers.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
void helper(vector<int>& freq, vector<int>& res, int now) {
if(100 <= now and now <= 999) {
if(now % 2 == 0) res.push_back(now);
return;
}
for(int i = !now; i < 10; i++) {
if(!freq[i]) continue;
freq[i]--;
helper(freq,res,now * 10 + i);
freq[i]++;
}
}
public:
vector<int> findEvenNumbers(vector<int>& digits) {
vector<int> freq(10), res;
for(auto& d : digits) freq[d]++;
helper(freq,res,0);
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/05/12/PS/LeetCode/finding-3-digit-even-numbers/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.