Form Smallest Number From Two Digit Arrays
EasyUpdated: Aug 2, 2025
Practice on:
Problem
Given two arrays of unique digits nums1 and nums2, return thesmallest number that contains at least one digit from each array.
Examples
Example 1
Input: nums1 = [4,1,3], nums2 = [5,7]
Output: 15
Explanation: The number 15 contains the digit 1 from nums1 and the digit 5 from nums2. It can be proven that 15 is the smallest number we can have.
Example 2
Input: nums1 = [3,5,2,6], nums2 = [3,1,7]
Output: 3
Explanation: The number 3 contains the digit 3 which exists in both arrays.
Constraints
1 <= nums1.length, nums2.length <= 91 <= nums1[i], nums2[i] <= 9- All digits in each array are unique.
Solution
Method 1 – Brute Force and Set Intersection
Intuition
The smallest number containing at least one digit from each array can be formed by either:
- A two-digit number using the smallest digit from each array (in both orders), or
- The smallest common digit in both arrays (as a one-digit number).
Approach
- Find the smallest digit in
nums1andnums2. - Check if there is any common digit between the two arrays. If so, the smallest such digit is the answer.
- Otherwise, return the smaller of the two possible two-digit numbers formed by combining the smallest digits from each array.
Code
C++
class Solution {
public:
int minNumber(vector<int>& nums1, vector<int>& nums2) {
int ans = 100;
for (int x : nums1)
for (int y : nums2)
if (x == y) ans = min(ans, x);
else ans = min(ans, min(x * 10 + y, y * 10 + x));
return ans;
}
};
Go
type Solution struct{}
func (Solution) minNumber(nums1, nums2 []int) int {
ans := 100
for _, x := range nums1 {
for _, y := range nums2 {
if x == y {
if x < ans { ans = x }
} else {
if x*10+y < ans { ans = x*10 + y }
if y*10+x < ans { ans = y*10 + x }
}
}
}
return ans
}
Java
class Solution {
public int minNumber(int[] nums1, int[] nums2) {
int ans = 100;
for (int x : nums1)
for (int y : nums2)
if (x == y) ans = Math.min(ans, x);
else ans = Math.min(ans, Math.min(x * 10 + y, y * 10 + x));
return ans;
}
}
Kotlin
class Solution {
fun minNumber(nums1: IntArray, nums2: IntArray): Int {
var ans = 100
for (x in nums1) {
for (y in nums2) {
if (x == y) ans = minOf(ans, x)
else ans = minOf(ans, x * 10 + y, y * 10 + x)
}
}
return ans
}
}
Python
class Solution:
def minNumber(self, nums1: list[int], nums2: list[int]) -> int:
ans = 100
for x in nums1:
for y in nums2:
if x == y:
ans = min(ans, x)
else:
ans = min(ans, x * 10 + y, y * 10 + x)
return ans
Rust
impl Solution {
pub fn min_number(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
let mut ans = 100;
for &x in &nums1 {
for &y in &nums2 {
if x == y {
ans = ans.min(x);
} else {
ans = ans.min(x * 10 + y).min(y * 10 + x);
}
}
}
ans
}
}
TypeScript
class Solution {
minNumber(nums1: number[], nums2: number[]): number {
let ans = 100;
for (const x of nums1) {
for (const y of nums2) {
if (x === y) ans = Math.min(ans, x);
else ans = Math.min(ans, x * 10 + y, y * 10 + x);
}
}
return ans;
}
}
Complexity
- ⏰ Time complexity:
O(m * n), wheremandnare the lengths ofnums1andnums2, since all pairs are checked. - 🧺 Space complexity:
O(1), as only a few variables are used.