Form Smallest Number From Two Digit Arrays - Problem
You're given two arrays of unique digits nums1 and nums2. Your task is to find the smallest possible number that contains at least one digit from each array.
The key insight is that we want the smallest number, so we should prioritize:
- Single-digit numbers over multi-digit numbers
- Smaller digits over larger digits
- When forming multi-digit numbers, place the smaller digit first
Example: If nums1 = [4, 1, 3] and nums2 = [5, 7], we could form numbers like 45, 15, 47, etc. The smallest would be 15.
Input & Output
example_1.py โ Basic Case
$
Input:
nums1 = [4,1,3], nums2 = [5,7]
โบ
Output:
15
๐ก Note:
No common digits exist. The smallest two-digit number is formed by taking min(nums1)=1 and min(nums2)=5, giving us min(15, 51) = 15.
example_2.py โ Common Digit
$
Input:
nums1 = [3,5,2,6], nums2 = [3,1,7]
โบ
Output:
3
๐ก Note:
Both arrays contain digit 3, so the smallest possible number is the single digit 3.
example_3.py โ Multiple Common Digits
$
Input:
nums1 = [6,8,4,2], nums2 = [4,9,2,3]
โบ
Output:
2
๐ก Note:
Arrays share digits 4 and 2. The minimum common digit is 2, which gives us the smallest possible number.
Visualization
Tap to expand
Understanding the Visualization
1
Check for Shared Jersey Numbers
First priority: if any players from both teams have the same jersey number, that's our single-digit team number
2
Find Team Captains (Minimum Numbers)
If no shared numbers, identify the player with the lowest jersey number from each team
3
Form Optimal Team Number
Arrange the two captain numbers to create the smallest possible two-digit team number
4
Tournament Ready!
The resulting number gives us the best possible seeding position
Key Takeaway
๐ฏ Key Insight: Single-digit solutions (common elements) always beat two-digit solutions, so check for intersections first!
Time & Space Complexity
Time Complexity
O(n + m)
Single pass through each array to create sets and find minimums
โ Linear Growth
Space Complexity
O(n + m)
Space for two hash sets to store unique digits
โก Linearithmic Space
Constraints
- 1 โค nums1.length, nums2.length โค 9
- 1 โค nums1[i], nums2[i] โค 9
- All digits in each array are unique
- Arrays contain only single digits (0-9)
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code