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
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
temp.f95:5:34: 5 | integer, dimension(n) :: nums1 = [4, 1, 3], nums2 = [5, 7] | 1 Error: Different shape for array assignment at (1) on dimension 1 (9 and 3) temp.f95:24:29: 24 | min_num = huge(1_I8P) | 1 Error: Missing kind-parameter at (1) temp.f95:36:34: 36 | if (min_num == huge(1_I8P)) then | 1 Error: Missing kind-parameter at (1) temp.f95:38:11: 38 | end if | 1 Error: Expecting END FUNCTION statement at (1) temp.f95:9:25: 9 | min_num = solve(nums1, nums2) | 1 Error: Symbol ‘nums1’ at (1) has no IMPLICIT type temp.f95:9:32: 9 | min_num = solve(nums1, nums2) | 1 Error: Symbol ‘nums2’ at (1) has no IMPLICIT type
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
temp.f95:7:24: 7 | function smallest_number(nums1, nums2) result(smallest) | 1 Error: MODULE attribute of ‘smallest_number’ conflicts with PROCEDURE attribute at (1) temp.f95:9:41: 9 | integer, intent(in) :: nums1(:), nums2(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:10:19: 10 | integer :: smallest | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:12:22: 12 | integer :: i, j, digit | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:13:16: 13 | logical :: found | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:15:13: 15 | smallest = 10 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:17:21: 17 | do i = 1, size(nums1) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:18:25: 18 | do j = 1, size(nums2) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:19:35: 19 | digit = nums1(i) + nums2(j) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:20:34: 20 | if (digit < smallest) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:21:28: 21 | smallest = digit | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:22:26: 22 | found = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:23:11: 23 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:24:7: 24 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:25:3: 25 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:27:21: 27 | if (.not. found) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:28:16: 28 | smallest = 0 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:29:3: 29 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:31:3: 31 | end function smallest_number | 1 Error: Expecting END MODULE statement at (1) temp.f95:37:5: 37 | use smallest_number | 1 Fatal Error: Cannot open module file ‘smallest_number.mod’ for reading at (1): No such file or directory compilation terminated.
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.
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.