DDSA Solutions

2094. Finding 3-Digit Even Numbers

Time: O(1000)
Space: O(1)
Advertisement

Approach

Count digit frequencies; enumerate all 3-digit even numbers and check feasibility with counts.

Key Techniques

Array

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 Table

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

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³¹.

2094.cs
C#
// 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();
    }
}
Advertisement
Was this solution helpful?