Given two integer arrays nums1
and nums2
, sorted in non-decreasing order, return the minimum integer common to both arrays. If there is no common integer amongst nums1
and nums2
, return -1
.
Note that an integer is said to be common to nums1
and nums2
if both arrays have at least one occurrence of that integer.
Example 1:
Input: nums1 = [1,2,3], nums2 = [2,4] Output: 2 Explanation: The smallest element common to both arrays is 2, so we return 2.
Example 2:
Input: nums1 = [1,2,3,6], nums2 = [2,3,4,5] Output: 2 Explanation: There are two common elements in the array 2 and 3 out of which 2 is the smallest, so 2 is returned.
Constraints:
1 <= nums1.length, nums2.length <= 105
1 <= nums1[i], nums2[j] <= 109
nums1
and nums2
are sorted in non-decreasing order.program min_integer_common
implicit none
integer, parameter :: n1 = 3, n2 = 4
integer, dimension(n1) :: nums1 = [1, 2, 3]
integer, dimension(n2) :: nums2 = [2, 4]
integer :: i, j, k, l, m, n, result
! Example 1
write (*, '(A, I0)') 'Example 1: ', min_integer_common(nums1, nums2)
! Example 2
nums1 = [1, 2, 3, 6]
nums2 = [2, 3, 4, 5]
write (*, '(A, I0)') 'Example 2: ', min_integer_common(nums1, nums2)
contains
function min_integer_common(nums1, nums2) result(result)
implicit none
integer, intent(in) :: nums1(:), nums2(:)
integer :: result
logical :: found
result = -1
do i = 1, size(nums1)
found = .false.
do j = 1, size(nums2)
if (nums1(i) == nums2(j)) then
found = .true.
exit
end if
end do
if (found) then
result = max(result, nums1(i))
end if
end do
end function min_integer_common
end program min_integer_common
temp.f95:6:35: 6 | integer, dimension(n2) :: nums2 = [2, 4] | 1 Error: Different shape for array assignment at (1) on dimension 1 (4 and 2) temp.f95:10:59: 10 | write (*, '(A, I0)') 'Example 1: ', min_integer_common(nums1, nums2) | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:15:59: 15 | write (*, '(A, I0)') 'Example 2: ', min_integer_common(nums1, nums2) | 1 Error: Symbol at (1) is not appropriate for an expression temp.f95:19:60: 19 | function min_integer_common(nums1, nums2) result(result) | 1 Error: PROGRAM attribute of โmin_integer_commonโ conflicts with PROCEDURE attribute at (1) temp.f95:20:21: 20 | implicit none | 1 Error: Unexpected IMPLICIT NONE statement in CONTAINS section at (1) temp.f95:21:49: 21 | integer, intent(in) :: nums1(:), nums2(:) | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:22:25: 22 | integer :: result | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:23:24: 23 | logical :: found | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:25:19: 25 | result = -1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:26:29: 26 | do i = 1, size(nums1) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:27:27: 27 | found = .false. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:28:33: 28 | do j = 1, size(nums2) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:29:38: 29 | if (nums1(i) == nums2(j)) then | 1 Error: Syntax error in IF-expression at (1) temp.f95:30:34: 30 | found = .true. | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:31:24: 31 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:32:19: 32 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:33:15: 33 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:34:27: 34 | if (found) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:35:46: 35 | result = max(result, nums1(i)) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:36:15: 36 | end if | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:37:11: 37 | end do | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:38:7: 38 | end function min_integer_common | 1 Error: Expecting END PROGRAM statement at (1) temp.f95:14:9: 14 | nums2 = [2, 3, 4, 5] | 1 Error: Symbol โnums2โ at (1) has no IMPLICIT type; did you mean โnums1โ? temp.f95:13:4: 13 | nums1 = [1, 2, 3, 6] | 1 Error: Different shape for array assignment at (1) on dimension 1 (3 and 4)
! Declare the input and output variables
integer, dimension(100000) :: nums1, nums2
integer :: n1, n2, i, j, common
! Read the input
read(*,*) n1, nums1
read(*,*) n2, nums2
! Initialize the output
common = -1
! Loop through the arrays and find the common elements
do i = 1, n1
do j = 1, n2
if (nums1(i) == nums2(j)) then
common = nums1(i)
exit
end if
end do
end do
! Output the result
write(*,*) common
end
At line 6 of file temp.f95 (unit = 5, file = 'stdin') Fortran runtime error: End of file Error termination. Backtrace: #0 0x7842ceda3960 in ??? #1 0x7842ceda44d9 in ??? #2 0x7842ceff817b in ??? #3 0x7842ceff1684 in ??? #4 0x7842ceff22aa in ??? #5 0x5b3da5bb4219 in MAIN__ #6 0x5b3da5bb44fd in main
def min_common_number(nums1, nums2):
i, j = 0, 0
while i < len(nums1) and j < len(nums2):
if nums1[i] == nums2[j]:
return nums1[i]
elif nums1[i] < nums2[j]:
i += 1
else:
j += 1
return -1
The algorithm is pretty simple. We use two pointers i and j to iterate through nums1 and nums2. As they are sorted in non-decreasing order, we can simply compare the elements at i and j. If nums1[i] is equal to nums2[j], we return the current element as it is the minimum common integer. If nums1[i] is less than nums2[j], we increment i. Otherwise, we increment j. If we reach the end of any array, we break the loop and return -1 as no common integer was found.
int minCommonNumber(vector<int>& nums1, vector<int>& nums2) {
int i = 0, j = 0;
while (i < nums1.size() && j < nums2.size()) {
if (nums1[i] == nums2[j]) {
return nums1[i];
} else if (nums1[i] < nums2[j]) {
i++;
} else {
j++;
}
}
return -1;
}
The algorithm is pretty simple. We use two pointers i and j to iterate through nums1 and nums2. As they are sorted in non-decreasing order, we can simply compare the elements at i and j. If nums1[i] is equal to nums2[j], we return the current element as it is the minimum common integer. If nums1[i] is less than nums2[j], we increment i. Otherwise, we increment j. If we reach the end of any array, we break the loop and return -1 as no common integer was found.