2094. Finding 3-Digit Even Numbers
Approach
Count digit frequencies; enumerate all 3-digit even numbers and check feasibility with counts.
Key Techniques
Array problems involve manipulating elements stored in a contiguous block of memory. Key techniques include two-pointer traversal, prefix sums, sliding windows, and in-place partitioning. In C#, arrays are zero-indexed and fixed in size — use List<T> when you need dynamic resizing.
Hash tables provide O(1) average-case lookup, insert, and delete. They are the go-to tool for counting frequencies, detecting complements (Two Sum pattern), and caching seen values. In C#, use Dictionary<K,V> for maps and HashSet<T> for membership checks.
Math problems test number theory, combinatorics, and modular arithmetic. Common tools: GCD/LCM (Euclidean algorithm), prime sieve, modular inverse (Fermat's little theorem), digit manipulation, and bit tricks. Overflow is a key concern in C# — use long when products may exceed 2³¹.
// Approach: Count digit frequencies; enumerate all 3-digit even numbers and check feasibility with counts.
// Time: O(1000) Space: O(1)
public class Solution
{
public int[] FindEvenNumbers(int[] digits)
{
List<int> ans = new List<int>();
int[] count = new int[10];
foreach (var digit in digits)
++count[digit];
// Try to construct `abc`.
for (int a = 1; a <= 9; ++a)
{
for (int b = 0; b <= 9; ++b)
{
for (int c = 0; c <= 8; c += 2)
{
if (count[a] > 0 && count[b] > (b == a ? 1 : 0) &&
count[c] > (c == a ? 1 : 0) + (c == b ? 1 : 0))
ans.Add(a * 100 + b * 10 + c);
}
}
}
return ans.ToArray();
}
}