Finding 3-Digit Even Numbers - Problem

You are given an integer array digits, where each element is a digit. The array may contain duplicates.

You need to find all the unique integers that follow the given requirements:

  • The integer consists of the concatenation of three elements from digits in any arbitrary order.
  • The integer does not have leading zeros.
  • The integer is even.

For example, if the given digits were [1, 2, 3], integers 132 and 312 follow the requirements.

Return a sorted array of the unique integers.

Input & Output

Example 1 — Basic Case
$ Input: digits = [2,1,3,0]
Output: [102,120,130,132,210,230,310,312]
💡 Note: All valid 3-digit even numbers using digits from [2,1,3,0]: First digit cannot be 0, last digit must be even (0 or 2). Numbers like 102, 120, 130, etc. are formed and sorted.
Example 2 — Limited Even Digits
$ Input: digits = [2,7,4]
Output: [274,472,724,742]
💡 Note: Even digits available: 2, 4. Valid combinations: 274, 472, 724, 742. All start with non-zero and end with even digit.
Example 3 — No Valid Numbers
$ Input: digits = [1,3,5]
Output: []
💡 Note: All digits are odd, so no 3-digit even numbers can be formed. The last digit must be even.

Constraints

  • 1 ≤ digits.length ≤ 100
  • 0 ≤ digits[i] ≤ 9

Visualization

Tap to expand
Finding 3-Digit Even Numbers INPUT digits array 2 1 3 0 [0] [1] [2] [3] Requirements: 1. 3 digits concatenated 2. No leading zeros 3. Must be EVEN 4. Unique integers only Even digits in array: 0, 2 ALGORITHM STEPS 1 Count Digit Frequency Count occurrences of 0-9 2 Check Even Endings Last digit must be 0,2,4,6,8 3 Build Valid Numbers First digit: 1-9 (no zero) 4 Validate Availability Check digit counts suffice Example: Building 102 1 100s 0 10s 2 EVEN = 102 Last digit checked for evenness FINAL RESULT Sorted valid 3-digit even numbers: 102 120 130 132 210 230 310 312 Output Array: [102,120,130,132, 210,230,310,312] OK - Validated 8 unique 3-digit even numbers All end in 0 or 2 No leading zeros Key Insight: Instead of generating all permutations and filtering, directly iterate through numbers 100-999. Check only even endings (0,2,4,6,8), verify first digit is not zero, and confirm digit availability. TutorialsPoint - Finding 3-Digit Even Numbers | Optimized - Direct Even Digit Check
Asked in
Amazon 25 Google 20 Microsoft 15
32.0K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen