Problem Description
Given an integer array of digits, form all unique 3-digit numbers by concatenating any three digits from the array. The number must not have a leading zero and must be even. Return the resulting numbers in sorted order.
Key Insights
- Permutations of three digits must be considered, but duplicates in the output should be avoided.
- The hundreds digit (first digit) cannot be zero.
- The units digit (last digit) must be even.
- Brute force enumeration (using three nested loops or permutation generation) is acceptable given the input constraints.
Space and Time Complexity
Time Complexity: O(n^3) in the worst-case scenario, where n is the number of digits. Space Complexity: O(k), where k is the number of valid 3-digit numbers stored.
Solution
We iterate through every possible combination of three digits (taking into account their indices to avoid using the same digit more times than allowed). For each combination:
- Check that the first digit is not zero to avoid a leading zero.
- Ensure that the final digit is even (i.e., 0, 2, 4, 6, or 8). If both conditions are met, combine the digits into a 3-digit number and add it to a set to ensure uniqueness. Finally, convert the set into a sorted list and return it. Data structures used include a set for unique values, and simple loops for enumeration.