Loading content...
You are given two integer arrays, sortedArr1 and sortedArr2, both arranged in non-decreasing order. Your task is to find and return the smallest integer that appears in both arrays.
If no such common element exists between the two arrays, return -1.
An element is considered shared (or common) between the two arrays if it appears at least once in both sortedArr1 and sortedArr2.
Key Insight: Since both arrays are already sorted in ascending order, you can leverage this property to design an efficient algorithm that avoids unnecessary comparisons.
sortedArr1 = [1,2,3]
sortedArr2 = [2,4]2The value 2 appears in both arrays. It is the only common element, hence the smallest shared element is 2.
sortedArr1 = [1,2,3,6]
sortedArr2 = [2,3,4,5]2Both 2 and 3 are present in both arrays. Since 2 is smaller than 3, we return 2 as the smallest shared element.
sortedArr1 = [1,3,5]
sortedArr2 = [2,4,6]-1The first array contains only odd numbers [1,3,5] while the second array contains only even numbers [2,4,6]. There is no element common to both arrays, so we return -1.
Constraints