All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g2501_2600.s2540_minimum_common_value.Solution Maven / Gradle / Ivy

There is a newer version: 1.38
Show newest version
package g2501_2600.s2540_minimum_common_value;

// #Easy #Array #Hash_Table #Binary_Search #Two_Pointers
// #2023_04_22_Time_0_ms_(100.00%)_Space_58.9_MB_(33.19%)

/**
 * 2540 - Minimum Common Value\.
 *
 * Easy
 *
 * 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.
**/
public class Solution {
    public int getCommon(int[] nums1, int[] nums2) {
        int i = 0;
        int j = 0;
        if (nums1[0] > nums2[nums2.length - 1] || nums1[nums1.length - 1] < nums2[0]) {
            return -1;
        }
        while (i < nums1.length && j < nums2.length) {
            if (nums1[i] == nums2[j]) {
                return nums1[i];
            }
            if (nums1[i] > nums2[j]) {
                j++;
            } else {
                i++;
            }
        }
        return -1;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy