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
  • Both nums1 and nums2 are sorted in non-decreasing order.

Note: This problem is from LeetCode.
โŒ Compiled
โŒ Executed
โŒ Correct
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
โœ… Compiled
โŒ Executed
โŒ Correct
! 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
๐ŸŒ Data from online sources
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.

๐ŸŒ Data from online sources
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.