#3483
Easy Algorithms Unique 3 digit even numbers
Array Hash Table Recursion Enumeration
69.2% acceptance
Feb 25, 2026
127
34
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.
Solution
Rust
Time O(n³)
Space O(n)
impl Solution {
pub fn total_numbers(digits: Vec<i32>) -> i32 {
use std::collections::HashSet;
let mut cnt = [0i32; 10];
for &d in &digits { cnt[d as usize] += 1; }
let mut seen = HashSet::new();
let mut ans = 0;
for a in 1..=9i32 {
if cnt[a as usize] == 0 { continue; }
cnt[a as usize] -= 1;
for b in 0..=9i32 {
if cnt[b as usize] == 0 { continue; }
cnt[b as usize] -= 1;
for c in (0..=8i32).step_by(2) {
if cnt[c as usize] == 0 { continue; }
let num = a * 100 + b * 10 + c;
if seen.insert(num) { ans += 1; }
}
cnt[b as usize] += 1;
}
cnt[a as usize] += 1;
}
ans
}
}