# 2605. Form Smallest Number From Two Digit Arrays

## Description

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

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 <= 9
• 1 <= nums1[i], nums2[i] <= 9
• All digits in each array are unique.

## Solutions

• class Solution {
public int minNumber(int[] nums1, int[] nums2) {
int mask1 = 0, mask2 = 0;
for (int x : nums1) {
mask1 |= 1 << x;
}
for (int x : nums2) {
mask2 |= 1 << x;
}
if (mask != 0) {
}
int a = Integer.numberOfTrailingZeros(mask1);
int b = Integer.numberOfTrailingZeros(mask2);
return Math.min(a * 10 + b, b * 10 + a);
}
}

• class Solution {
public:
int minNumber(vector<int>& nums1, vector<int>& nums2) {
int mask1 = 0, mask2 = 0;
for (int x : nums1) {
mask1 |= 1 << x;
}
for (int x : nums2) {
mask2 |= 1 << x;
}
}
int a = __builtin_ctz(mask1);
int b = __builtin_ctz(mask2);
return min(a * 10 + b, b * 10 + a);
}
};

• class Solution:
def minNumber(self, nums1: List[int], nums2: List[int]) -> int:
for x in nums1:
mask1 |= 1 << x
for x in nums2:
mask2 |= 1 << x
a = (mask1 & -mask1).bit_length() - 1
b = (mask2 & -mask2).bit_length() - 1
return min(a * 10 + b, b * 10 + a)


• func minNumber(nums1 []int, nums2 []int) int {
for _, x := range nums1 {
mask1 |= 1 << x
}
for _, x := range nums2 {
mask2 |= 1 << x
}
}
return min(a*10+b, b*10+a)
}

func min(a, b int) int {
if a < b {
return a
}
return b
}

• function minNumber(nums1: number[], nums2: number[]): number {
let mask1: number = 0;
let mask2: number = 0;
for (const x of nums1) {
mask1 |= 1 << x;
}
for (const x of nums2) {
mask2 |= 1 << x;
}
if (mask !== 0) {
}
const a = numberOfTrailingZeros(mask1);
const b = numberOfTrailingZeros(mask2);
return Math.min(a * 10 + b, b * 10 + a);
}

function numberOfTrailingZeros(i: number): number {
let y = 0;
if (i === 0) {
return 32;
}
let n = 31;
y = i << 16;
if (y != 0) {
n = n - 16;
i = y;
}
y = i << 8;
if (y != 0) {
n = n - 8;
i = y;
}
y = i << 4;
if (y != 0) {
n = n - 4;
i = y;
}
y = i << 2;
if (y != 0) {
n = n - 2;
i = y;
}
return n - ((i << 1) >>> 31);
}


• use std::collections::HashMap;

impl Solution {
pub fn min_number(nums1: Vec<i32>, nums2: Vec<i32>) -> i32 {
let mut h1: HashMap<i32, bool> = HashMap::new();

for &n in &nums1 {
h1.insert(n, true);
}

let mut h2: HashMap<i32, bool> = HashMap::new();
for &n in &nums2 {
h2.insert(n, true);
}

let mut a = 0;
let mut b = 0;
for i in 1..10 {
if h1.contains_key(&i) && h2.contains_key(&i) {
return i;
}

if a == 0 && h1.contains_key(&i) {
a = i;
}

if b == 0 && h2.contains_key(&i) {
b = i;
}
}

std::cmp::min(a * 10 + b, b * 10 + a)
}
}