Find the Integer Added to Array II - Problem
You are given two integer arrays nums1 and nums2. From nums1, two elements have been removed, and all other elements have been increased (or decreased) by an integer x.
As a result, nums1 becomes equal to nums2. Two arrays are considered equal when they contain the same integers with the same frequencies.
Return the minimum possible integer x that achieves this equivalence.
Input & Output
Example 1 — Basic Case
$
Input:
nums1 = [4,6,4,1,2], nums2 = [5,3,1]
›
Output:
-1
💡 Note:
Remove elements 4 and 2 from nums1 to get [6,4,1]. Then add x = -1 to each element: [6-1, 4-1, 1-1] = [5,3,0]. Wait, let me recalculate. Remove 4 and 6: [4,1,2]. Add x=1: [5,2,3]. Actually, remove the first 4 and the 2: [6,4,1]. Add x=-1: [5,3,0]. Let me try remove 6 and 2: [4,4,1]. This needs to match [5,3,1], so x=1 gives [5,5,2]. Remove 4 and 1: [4,6,2], add x=-1 gives [3,5,1]. Remove elements at indices that make [6,4,1] → add x=-1 → [5,3,0]. Actually remove elements 4,6 to get remaining [4,1,2] which with x=1 becomes [5,2,3]. The answer should be calculated by trying different removal combinations.
Example 2 — Remove Different Elements
$
Input:
nums1 = [3,5,1,3], nums2 = [2,4]
›
Output:
-1
💡 Note:
Remove 2 elements from [3,5,1,3] to get an array of size 2. Remove 3 and 1: remaining [5,3], add x=-1: [4,2]. Sort both: [2,4] = [2,4] ✓
Example 3 — Minimum Removal
$
Input:
nums1 = [1,2], nums2 = []
›
Output:
0
💡 Note:
Remove both elements from nums1 to get empty array, which matches empty nums2. No transformation needed, so x = 0
Constraints
- 3 ≤ nums1.length ≤ 200
- nums1.length - 2 = nums2.length
- -1000 ≤ nums1[i], nums2[i] ≤ 1000
Visualization
Tap to expand
Understanding the Visualization
1
Input
nums1 = [4,6,4,1,2], nums2 = [5,3,1] (target)
2
Process
Remove 2 elements from nums1, then add x to each remaining element
3
Output
Find minimum x that makes transformation work
Key Takeaway
🎯 Key Insight: After sorting, we only need to check 3 strategic removal patterns instead of all possible combinations
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code