Form Smallest Number From Two Digit Arrays

🏠 ⬅️ ➡️

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.

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer, parameter :: n = 9
    integer, dimension(n) :: nums1 = [4, 1, 3], nums2 = [5, 7]
    integer :: i, j, k, l, m, n1, n2, min_num

    ! solve the problem
    min_num = solve(nums1, nums2)

    ! print the result
    write (*, '(A, I0)') 'The smallest number containing at least one digit from each array is: ', min_num

contains

    function solve(nums1, nums2) result(min_num)
        implicit none
        integer, intent(in) :: nums1(:), nums2(:)
        integer :: i, j, k, l, m, n1, n2, min_num

        ! initialize variables
        n1 = size(nums1)
        n2 = size(nums2)
        min_num = huge(1_I8P)

        ! loop over all possible combinations of digits
        do i = 1, n1
            do j = 1, n2
                if (nums1(i) == nums2(j)) then
                    min_num = min(min_num, nums1(i)*10 + nums2(j))
                end if
            end do
        end do

        ! check if there is no solution
        if (min_num == huge(1_I8P)) then
            stop "No solution"
        end if

        ! return the minimum number
        min_num = min_num - mod(min_num, 10)

    end function solve

end program main
Compiled
Executed
Correct
module smallest_number

implicit none

contains

function smallest_number(nums1, nums2) result(smallest)

integer, intent(in) :: nums1(:), nums2(:)
integer :: smallest

integer :: i, j, digit
logical :: found

smallest = 10

do i = 1, size(nums1)
    do j = 1, size(nums2)
        digit = nums1(i) + nums2(j)
        if (digit < smallest) then
            smallest = digit
            found = .true.
        end if
    end do
end do

if (.not. found) then
    smallest = 0
end if

end function smallest_number

end module smallest_number

program test_smallest_number

use smallest_number

implicit none

integer :: nums1(3) = [4, 1, 3]
integer :: nums2(2) = [5, 7]
integer :: smallest

smallest = smallest_number(nums1, nums2)

write (*,*) smallest

nums1 = [3, 5, 2, 6]
nums2 = [3, 1, 7]

smallest = smallest_number(nums1, nums2)

write (*,*) smallest

end program test_smallest_number
🌐 Data from online sources
def smallest_number_with_at_least_one_digit_from_each_array(nums1, nums2):
    min1 = min(nums1)
    min2 = min(nums2)
    for num1 in nums1:
        for num2 in nums2:
            if num1 == num2:
                return num1
    return min1 * 10 + min2

The algorithm works as follows: 1. Find the smallest numbers min1 and min2 in each input array nums1 and nums2 respectively. 2. Check if there is any identical number num1 and num2 in nums1 and nums2 respectively (num1 == num2). If found, return that shared number. This is because the problem states that we need the smallest number that contains at least one digit from each array, and a shared number will always be smaller than the result obtained by concatenating the smallest digits from each array (due to the unique digits constraint in the problem statement). 3. If no shared number is found, concatenate the smallest numbers of each array (min1 * 10 + min2). This gives the smallest possible number containing at least one digit from each array.

🌐 Data from online sources
int smallestNumberWithAtLeastOneDigitFromEachArray(vector<int>& nums1, vector<int>& nums2) {
    int min1 = *min_element(nums1.begin(), nums1.end());
    int min2 = *min_element(nums2.begin(), nums2.end());
    for (int num1 : nums1) {
        for (int num2 : nums2) {
            if (num1 == num2) {
                return num1;
            }
        }
    }
    return min1 * 10 + min2;
}

The algorithm works as follows: 1. Find the smallest numbers min1 and min2 in each input array nums1 and nums2 respectively. 2. Check if there is any identical number num1 and num2 in nums1 and nums2 respectively (num1 == num2). If found, return that shared number. This is because the problem states that we need the smallest number that contains at least one digit from each array, and a shared number will always be smaller than the result obtained by concatenating the smallest digits from each array (due to the unique digits constraint in the problem statement). 3. If no shared number is found, concatenate the smallest numbers of each array (min1 * 10 + min2). This gives the smallest possible number containing at least one digit from each array.