Skip to main content

Unique 3-Digit Even Numbers - Solution & Explanation

EasyArrayHash TableRecursionEnumeration7 min readAsked at: Amazon, Google, Bloomberg
Practice this problem

Problem Statement

You are given an array of digits called digits. Your task is to determine the number of distinct three-digit even numbers that can be formed using these digits.

Note: Each copy of a digit can only be used once per number, and there may not be leading zeros.

 

Example 1:

Input: digits = [1,2,3,4]

Output: 12

Explanation: The 12 distinct 3-digit even numbers that can be formed are 124, 132, 134, 142, 214, 234, 312, 314, 324, 342, 412, and 432. Note that 222 cannot be formed because there is only 1 copy of the digit 2.

Example 2:

Input: digits = [0,2,2]

Output: 2

Explanation: The only 3-digit even numbers that can be formed are 202 and 220. Note that the digit 2 can be used twice because it appears twice in the array.

Example 3:

Input: digits = [6,6,6]

Output: 1

Explanation: Only 666 can be formed.

Example 4:

Input: digits = [1,3,5]

Output: 0

Explanation: No even 3-digit numbers can be formed.

 

Constraints:

  • 3 <= digits.length <= 10
  • 0 <= digits[i] <= 9

Approach Overview

Problem Overview: You are given an array of digits. The task is to form all possible unique three-digit even numbers using these digits, where each digit index can be used at most once. The number cannot start with 0, and the last digit must be even.

Approach 1: Brute Force Permutation (O(n^3) time, O(k) space)

Generate every ordered triple of indices (i, j, k) from the digits array. Each triple represents a three-digit number digits[i] * 100 + digits[j] * 10 + digits[k]. Apply the constraints while iterating: the first digit cannot be 0, and the last digit must be even. Since different index combinations can produce the same number, store valid results in a HashSet to keep them unique. This approach is straightforward because the number has fixed length (3), so checking every combination is feasible.

The algorithm runs three nested loops over the input array, giving O(n^3) time complexity. The set storing valid numbers takes O(k) space where k is the number of valid results (at most a few hundred). This brute-force enumeration works well because the search space is small.

Approach 2: Hash Set + Enumeration (O(n^3) time, O(k) space)

A cleaner implementation still enumerates digit positions but focuses on building the number directly while enforcing constraints early. Iterate over all possible first digits from the array, skipping 0. For the second digit, iterate again while ensuring a different index is used. For the third digit, only consider digits that are even (0,2,4,6,8). Construct the number and insert it into a HashSet to avoid duplicates.

The key idea is pruning invalid candidates during enumeration instead of generating all permutations first. By filtering on the last digit being even and preventing leading zeros, unnecessary checks are avoided. The final result can be returned as a sorted list of the unique values from the set.

This technique is essentially controlled enumeration combined with constant-time duplicate removal using a hash table. The digits are stored in an array, and iteration builds valid candidates efficiently.

Recommended for interviews: The Hash Set + Enumeration approach is what most interviewers expect. It demonstrates that you understand constraint filtering (no leading zero, even last digit) and how to guarantee uniqueness using a hash set. A naive permutation discussion shows baseline reasoning, but the enumeration approach with pruning and hashing signals stronger problem-solving skills.

Solution

We use a hash set s to record all distinct three-digit even numbers, and then enumerate all possible three-digit even numbers to add them to the hash set.

Finally, we return the size of the hash set.

The time complexity is O(n^3), and the space complexity is O(n^3). Where n is the length of the array digits.

Code

Python

Java

C++

Go

TypeScript

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Brute Force PermutationO(n^3)O(k)Small input sizes where generating all index combinations is simple and easy to implement
Hash Set + EnumerationO(n^3)O(k)Best general approach. Filters invalid candidates early and guarantees uniqueness with a hash set

Video Solution

3483. Unique 3-Digit Even Numbers | Leetcode Biweekly Contest 152 | Easy Approach #coding • Kabil Bano • 984 views views

Watch 4 more video solutions →

Frequently Asked Questions

Is Unique 3-Digit Even Numbers easy or hard?
Unique 3-Digit Even Numbers is classified as an Easy problem. The main challenge is correctly applying the constraints: no leading zero, digits used once per number, and the final digit must be even. Once those checks are implemented, the enumeration solution is straightforward.
Unique 3-Digit Even Numbers Python/Java solution
In Python or Java, iterate over all index triples, skip cases where the first digit is 0 or the last digit is odd, and build the number using arithmetic (hundreds, tens, ones). Insert each valid number into a HashSet or set. Finally convert the set to a sorted list or array before returning.
How to solve Unique 3-Digit Even Numbers in O(n)?
An O(n) solution is generally not feasible because the algorithm must consider combinations of three digits. The practical approach enumerates triples of indices with O(n^3) time. Early filtering (skip leading zero and enforce even last digit) reduces unnecessary checks but does not change the asymptotic complexity.
What is the best approach for Unique 3-Digit Even Numbers?
The most practical approach is Hash Set + Enumeration. Iterate over all triples of indices to construct three-digit numbers while enforcing constraints such as no leading zero and an even last digit. Insert each valid number into a hash set to guarantee uniqueness. This approach runs in O(n^3) time and O(k) space.
Is Unique 3-Digit Even Numbers asked at Google/Amazon/Meta?
Problems like this frequently appear in coding interviews at large tech companies because they test enumeration, constraint filtering, and hash set usage. Variants involving digit permutations and uniqueness checks are common in interview question banks used by companies such as Amazon and Google.
What data structure is used in Unique 3-Digit Even Numbers?
A hash set is the main data structure used to store generated numbers and automatically remove duplicates. The digits are provided in an array, and nested iteration enumerates valid combinations while the set guarantees uniqueness.
What is the time complexity of Unique 3-Digit Even Numbers?
The typical solution runs in O(n^3) time because three nested loops enumerate all possible digit positions. Since the number length is fixed at three, the cubic complexity is acceptable. Space complexity is O(k) for storing unique results in a hash set.

Ready to solve this problem?

Practice Unique 3-Digit Even Numbers with our built-in code editor and test cases.

Practice on FleetCode