Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Build confidence with an intuition-first walkthrough focused on array fundamentals.
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 <= 100 <= digits[i] <= 9Problem summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map
[1,2,3,4]
[0,2,2]
[6,6,6]
finding-3-digit-even-numbers)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3483: Unique 3-Digit Even Numbers
class Solution {
public int totalNumbers(int[] digits) {
Set<Integer> s = new HashSet<>();
int n = digits.length;
for (int i = 0; i < n; ++i) {
if (digits[i] % 2 == 1) {
continue;
}
for (int j = 0; j < n; ++j) {
if (i == j) {
continue;
}
for (int k = 0; k < n; ++k) {
if (digits[k] == 0 || k == i || k == j) {
continue;
}
s.add(digits[k] * 100 + digits[j] * 10 + digits[i]);
}
}
}
return s.size();
}
}
// Accepted solution for LeetCode #3483: Unique 3-Digit Even Numbers
func totalNumbers(digits []int) int {
s := make(map[int]struct{})
for i, a := range digits {
if a%2 == 1 {
continue
}
for j, b := range digits {
if i == j {
continue
}
for k, c := range digits {
if c == 0 || k == i || k == j {
continue
}
s[c*100+b*10+a] = struct{}{}
}
}
}
return len(s)
}
# Accepted solution for LeetCode #3483: Unique 3-Digit Even Numbers
class Solution:
def totalNumbers(self, digits: List[int]) -> int:
s = set()
for i, a in enumerate(digits):
if a & 1:
continue
for j, b in enumerate(digits):
if i == j:
continue
for k, c in enumerate(digits):
if c == 0 or k in (i, j):
continue
s.add(c * 100 + b * 10 + a)
return len(s)
// Accepted solution for LeetCode #3483: Unique 3-Digit Even Numbers
fn total_numbers(digits: Vec<i32>) -> i32 {
use std::collections::HashSet;
let len = digits.len();
let mut s = HashSet::new();
for i in 0..len {
if digits[i] == 0 {
continue;
}
for j in 0..len {
if i == j {
continue;
}
for k in 0..len {
if k == i || k == j || digits[k] % 2 == 1 {
continue;
}
s.insert(digits[i] * 100 + digits[j] * 10 + digits[k]);
}
}
}
s.len() as i32
}
fn main() {
let digits = vec![6, 6, 6];
let ret = total_numbers(digits);
println!("ret={ret}");
}
#[test]
fn test() {
{
let digits = vec![1, 2, 3, 4];
let ret = total_numbers(digits);
assert_eq!(ret, 12);
}
{
let digits = vec![0, 2, 2];
let ret = total_numbers(digits);
assert_eq!(ret, 2);
}
{
let digits = vec![6, 6, 6];
let ret = total_numbers(digits);
assert_eq!(ret, 1);
}
{
let digits = vec![1, 3, 5];
let ret = total_numbers(digits);
assert_eq!(ret, 0);
}
}
// Accepted solution for LeetCode #3483: Unique 3-Digit Even Numbers
function totalNumbers(digits: number[]): number {
const s = new Set<number>();
const n = digits.length;
for (let i = 0; i < n; ++i) {
if (digits[i] % 2 === 1) {
continue;
}
for (let j = 0; j < n; ++j) {
if (i === j) {
continue;
}
for (let k = 0; k < n; ++k) {
if (digits[k] === 0 || k === i || k === j) {
continue;
}
s.add(digits[k] * 100 + digits[j] * 10 + digits[i]);
}
}
}
return s.size;
}
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.