Leetcode 2605: Form Smallest Number From Two Digit Arrays

grid47
grid47
Exploring patterns and algorithms
Feb 20, 2024 6 min read

You are given two arrays nums1 and nums2, both containing unique digits. Your task is to find the smallest possible number that contains at least one digit from both arrays. If there are common digits between the two arrays, the smallest common digit will be the answer. If no common digit exists, return the smallest number formed by taking the smallest digit from each array.
Problem
Approach
Steps
Complexity
Input: You are given two arrays nums1 and nums2. Both arrays consist of unique digits between 1 and 9.
Example: nums1 = [8, 2, 5], nums2 = [6, 1]
Constraints:
• 1 <= nums1.length, nums2.length <= 9
• 1 <= nums1[i], nums2[i] <= 9
• All digits in each array are unique.
Output: Return the smallest number that contains at least one digit from each array. If there is a common digit between the two arrays, return it. If not, return the smallest number formed by combining the smallest digit from each array.
Example: Output: 15
Constraints:
• Output should be a single integer.
Goal: To find the smallest number containing at least one digit from each array.
Steps:
• Check if there is any common digit between nums1 and nums2.
• If there is a common digit, return that digit as the result.
• If no common digit exists, return the smallest number formed by combining the smallest digits from both arrays.
Goal: Ensure that the solution works efficiently with small arrays, as both arrays contain at most 9 elements.
Steps:
• nums1.length, nums2.length <= 9
• nums1[i], nums2[i] <= 9
Assumptions:
• The arrays nums1 and nums2 will always contain unique digits.
Input: nums1 = [8, 2, 5], nums2 = [6, 1]
Explanation: There are no common digits between the arrays. The smallest number formed by combining the smallest digits from nums1 and nums2 is 15, which is the correct answer.

Input: nums1 = [1, 2, 3], nums2 = [3, 4, 5]
Explanation: The common digit between the two arrays is 3. Therefore, the smallest number that contains a digit from both arrays is 3.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus