Minimum Index Sum of Common Elements
You are given two integer arrays nums1 and nums2, each of length n.
A pair of indices (i, j) is considered valid if: nums1[i] == nums2[j]
Your task is to compute the minimum value of i + j across all valid pairs.
If the two arrays share no common element, return -1.
Example 1:
Input: nums1 = [4, 7, 2, 9], nums2 = [5, 2, 8, 4]
Output: 3
Explanation:
Common elements → {4, 2}
- For 4 → (i, j) = (0, 3) → sum = 3
- For 2 → (i, j) = (2, 1) → sum = 3
Minimum index sum = 3
Example 2:
Input: nums1 = [10, 3, 6], nums2 = [1, 6, 3]
Output: 3
Explanation:
Common elements → {3, 6}
- For 3 → (1, 2) → sum = 3
- For 6 → (2, 1) → sum = 3
Minimum index sum = 3
Now Your Turn!
Pick the correct output for the given inputInput: nums1 = [8, 1, 5], nums2 = [5, 9, 1]
Still unsure what the problem is asking ?
Let’s go through a few more examples, step by step, to make it clearer.
Constraints:
- 1 ≤ n ≤ 105
- nums1.length == nums2.length == n
- -105 ≤ nums1[i], nums2[i] ≤ 105