Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created May 12, 2025 19:10
Show Gist options
  • Save Ifihan/473d0dd9a7e32795543ab18866af5b9e to your computer and use it in GitHub Desktop.
Save Ifihan/473d0dd9a7e32795543ab18866af5b9e to your computer and use it in GitHub Desktop.
Finding 3-Digit Even Numbers

Question

Approach

I iterated through all 3-digit numbers from 100 to 999, ensuring they are even (i.e., last digit is even). For each candidate, I checked if it could be formed using the available digits considering their counts. I used Counter to handle duplicates correctly, ensuring I only used digits as many times as they appear. I stored valid numbers in a set to avoid duplicates and finally returned them sorted.

Implementation

class Solution:
    def findEvenNumbers(self, digits: List[int]) -> List[int]:
        result = set()
        counter = Counter(digits)

        for i in range(100, 1000):
            d1, d2, d3 = i // 100, (i // 10) % 10, i % 10
            if d3 % 2 == 1:
                continue
            candidate_counter = Counter([d1, d2, d3])
            if all(candidate_counter[d] <= counter[d] for d in candidate_counter):
                result.add(i)

        return sorted(result)

Complexities

  • Time: O(1)
  • Space: O(1)
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment