Form Smallest Number From Two Digit Arrays - Problem

Given two arrays of unique digits nums1 and nums2, return the smallest number that contains at least one digit from each array.

The result can be formed by concatenating digits from both arrays in any order. You want to minimize the overall numerical value of the result.

Example: If nums1 = [4,1,3] and nums2 = [5,7], possible numbers include 14, 15, 17, 34, 35, 37, 41, 43, 45, 47, 51, 53, 54, 57, 71, 73, 74, 75. The smallest is 14.

Input & Output

Example 1 — No Common Digits
$ Input: nums1 = [4,1,3], nums2 = [5,7]
Output: 15
💡 Note: No digits appear in both arrays. The smallest from nums1 is 1, from nums2 is 5. Between 15 and 51, we choose 15.
Example 2 — Common Digit Exists
$ Input: nums1 = [3,5,2,6], nums2 = [3,1,7]
Output: 3
💡 Note: Digit 3 appears in both arrays. Since we can form a single-digit number, 3 is smaller than any 2-digit combination.
Example 3 — Multiple Common Digits
$ Input: nums1 = [6,8,4,2], nums2 = [4,9,2,3]
Output: 2
💡 Note: Both 4 and 2 appear in both arrays. We choose the smaller common digit: 2.

Constraints

  • 1 ≤ nums1.length, nums2.length ≤ 9
  • 1 ≤ nums1[i], nums2[i] ≤ 9
  • All digits in each array are unique

Visualization

Tap to expand
Form Smallest Number: Input → Strategy → OutputInput Arrays413nums157nums2Strategy DecisionCommon Digits?4,1,3 ∩ 5,7 = ∅No intersection found→ Use two smallest digitsmin(1,5) arranged as 15Final Output15Smallest possible2-digit number🎯 Key: Check intersection first, then optimize digit arrangement
Understanding the Visualization
1
Input Arrays
Two arrays of unique digits from 1-9
2
Find Strategy
Check for intersection or find minimums
3
Optimal Result
Return smallest possible number using both arrays
Key Takeaway
🎯 Key Insight: Always check for common digits first - a single shared digit beats any 2-digit combination!
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
9.0K Views
Medium Frequency
~8 min Avg. Time
324 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen