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 <= 1051 <= nums1[i], nums2[j] <= 109nums1 and nums2 are sorted in non-decreasing order.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method is like manually comparing every single item in two lists to see if any match. We go through each number in the first list and check if it exists anywhere in the second list. If we find a match, we've found our answer.
Here's how the algorithm would work step-by-step:
def minimum_common_value_brute_force(first_list, second_list):
for first_list_index in range(len(first_list)):
first_list_number = first_list[first_list_index]
# Need to iterate through the entire second list
for second_list_index in range(len(second_list)):
second_list_number = second_list[second_list_index]
# Found a common value, return it immediately
if first_list_number == second_list_number:
return first_list_number
# No common values were found
return -1The goal is to find the smallest number that appears in both lists. We can efficiently find this number by walking through both lists at the same time, like merging sorted lists. This avoids looking at unnecessary numbers and quickly finds the first shared value.
Here's how the algorithm would work step-by-step:
def find_minimum_common_value(first_list, second_list):
first_list_index = 0
second_list_index = 0
while first_list_index < len(first_list) and second_list_index < len(second_list):
#If value in first list is smaller, increment index
if first_list[first_list_index] < second_list[second_list_index]:
first_list_index += 1
#If value in second list is smaller, increment index
elif second_list[second_list_index] < first_list[first_list_index]:
second_list_index += 1
else:
#We found the minimum common value
return first_list[first_list_index]
return -1| Case | How to Handle |
|---|---|
| One or both input arrays are null | Throw IllegalArgumentException or return a specific error value (e.g., -1) to signal invalid input. |
| One or both input arrays are empty | Return -1 immediately, as there can be no common value. |
| Arrays contain only one element | Compare the single elements and return the value if they are equal, otherwise return -1. |
| Arrays contain large numbers (potential integer overflow) | Use long data type to perform comparisons to avoid integer overflow issues. |
| Arrays contain duplicate values | The algorithm should still find the minimum common value correctly, as it iterates through the arrays. |
| Arrays contain negative numbers | The algorithm should correctly handle negative numbers as long as comparisons are done appropriately. |
| No common value exists between the two arrays | Return -1 to indicate that no common value was found. |
| Arrays are very large, exceeding available memory | Consider a streaming approach where you read chunks of data from disk if the arrays are too large to fit in memory. |