Teaching Kids Programming – Finding 3-Digit Even Numbers (Breadth First Search Algorithm)


Teaching Kids Programming: Videos on Data Structures and Algorithms

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.

Example 1:
Input: digits = [2,1,3,0]
Output: [102,120,130,132,210,230,302,310,312,320]
Explanation: All the possible integers that follow the requirements are in the output array.
Notice that there are no odd integers or integers with leading zeros.

Example 2:
Input: digits = [2,2,8,8,2]
Output: [222,228,282,288,822,828,882]
Explanation: The same digit can be used as many times as it appears in digits.
In this example, the digit 8 is used twice each time in 288, 828, and 882.

Example 3:
Input: digits = [3,7,5]
Output: []
Explanation: No even integers can be formed using the given digits.

Constraints:
3 <= digits.length <= 100
0 <= digits[i] <= 9

The range of possible answers includes all even numbers between 100 and 999 inclusive. Could you check each possible answer to see if it could be formed from the digits in the array?

Finding 3-Digit Even Numbers (Breadth First Search Algorithm)

We can traversal the search tree via Breadth First Search (BFS) Algorithm. Along the BFS, we need to store the current value and the remaining digits that can be used to the queue. When it is 3 digit, we check and push the valid 3 digit even number to the answer, otherwise, we try putting next available digit on the right.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution:
    def findEvenNumbers(self, digits: List[int]) -> List[int]:
        ans = set()
        q = deque([(0, Counter(digits))])
        while q:
            n, c = q.popleft()
            if n > 99:
                if n & 1 == 0:
                    ans.add(n)
            else:
                for i in range(10):
                    if c[i] > 0:
                        x = copy.deepcopy(c)
                        x[i] -= 1
                        q.append((n * 10 + i, x))
        return sorted(ans)
class Solution:
    def findEvenNumbers(self, digits: List[int]) -> List[int]:
        ans = set()
        q = deque([(0, Counter(digits))])
        while q:
            n, c = q.popleft()
            if n > 99:
                if n & 1 == 0:
                    ans.add(n)
            else:
                for i in range(10):
                    if c[i] > 0:
                        x = copy.deepcopy(c)
                        x[i] -= 1
                        q.append((n * 10 + i, x))
        return sorted(ans)

The tuple of each node pushed to the queue contains the number and the remaining digits which is of type Counter. We need to make a copy (via deepcopy) of the counter and decrement the corresponding digit’s frequency by one.

The time/space complexity is O(P(N, 3)) aka Permutation of 3 digits out of N digits.

Finding 3-Digit Even Numbers from a Given List of Digits

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
667 words
Last Post: Teaching Kids Programming - Finding 3-Digit Even Numbers (Recursive Depth First Search Algorithm)
Next Post: Teaching Kids Programming - Count Pairs Of Similar Strings (Bruterforce, Hash Map / Counter and Bit masking)

The Permanent URL is: Teaching Kids Programming – Finding 3-Digit Even Numbers (Breadth First Search Algorithm)

Leave a Reply