Form Smallest Number From Two Digit Arrays

Here’s a Python solution:

1
2
3
4
5
6
7
class Solution:
    def minNumber(self, nums1: List[int], nums2: List[int]) -> int:
        intersection = set(nums1) & set(nums2)
        if intersection:
            return min(intersection)
        else:
            return min(min(nums1), min(nums2)) * 10 + max(min(nums1), min(nums2))

The first step is to check for common numbers in both arrays (intersection). If there is a common number, we return the smallest one, as it will be the smallest number that contains at least one digit from each array.

If there is no common number, then the smallest number that contains at least one digit from each array will be a two-digit number. This number is formed by the smallest number from each array, with the smaller one in the tens place and the larger one in the ones place. That’s why we multiply the smaller one by 10 and add the larger one.

Remember that the constraints in the problem state that all digits in each array are unique and range from 1 to 9. This means we don’t have to worry about zero as a potential digit.

Q&A

What’s wrong with my code?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
  public: int minNumber(vector & nums1, vector & nums2) {
    map < int, int > myMap;

    // for(int i =0; i<nums1.size(); i++)
    if (!nums1.empty()) {
      sort(nums1.begin(), nums1.end());
      int smallest1 = nums1[0];
      myMap.insert({
        smallest1,
        0
      });
    }

    if (!nums2.empty()) {
      sort(nums2.begin(), nums2.end());
      int smallest2 = nums2[0];
      myMap.insert({
        smallest2,
        0
      });

    }
    return myMap;

  }
};

Your code has several issues:

  1. You are returning a map myMap when the function is expected to return an int. This will cause a type mismatch error.

  2. In the problem statement, you’re asked to find the smallest number that contains at least one digit from each array. However, in your code, you’re simply sorting each array and storing the smallest element in a map, which doesn’t serve the purpose of the problem.

  3. The use of map in your code is not necessary. Your problem can be solved by using a simple comparison of elements in both arrays.

You need to restructure your code to find common elements in both arrays and return the smallest common element. If no common elements exist, you should return a two-digit number formed by the smallest numbers from both arrays.

Here’s a corrected version of your code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
class Solution {
public:
    int minNumber(vector<int>& nums1, vector<int>& nums2) {
        sort(nums1.begin(), nums1.end());
        sort(nums2.begin(), nums2.end());
        
        for (int i = 0; i < nums1.size(); ++i) {
            for (int j = 0; j < nums2.size(); ++j) {
                if (nums1[i] == nums2[j]) {
                    return nums1[i];
                }
            }
        }

        return min(nums1[0], nums2[0]) * 10 + max(nums1[0], nums2[0]);
    }
};

In this version, we first sort both arrays. Then we compare each element in both arrays to find the smallest common element. If there is no common element, we return a two-digit number composed of the smallest elements from both arrays.