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
Find the Integer Added to Array II INPUT nums1 (5 elements) 4 6 4 1 2 nums2 (3 elements) 5 3 1 Remove 2 elements from nums1 Add x to remaining elements Result should equal nums2 2 elements to remove ? ? ALGORITHM STEPS 1 Sort Both Arrays nums1: [1,2,4,4,6] nums2: [1,3,5] 2 Try 3 Cases Remove smallest 2, or 1st+3rd, or 2nd+3rd from nums1 3 Calculate x for each x = nums2[0] - nums1[kept] Case 1 [4,4,6]+x x=-3 FAIL Case 2 [2,4,6]+x x=-1 OK Case 3 [1,4,6]+x x=0 FAIL 4 Verify & Return Min Check if adding x makes remaining nums1 = nums2 [2,4,6] + (-1) = [1,3,5] Match! Return -1 FINAL RESULT Winning Case: Remove [1,4] 1 2 4 4 6 Kept: [2, 4, 6] Add x = -1 1 3 5 = nums2 [1, 3, 5] OUTPUT x = -1 Key Insight: Since we remove exactly 2 elements from sorted nums1, the first kept element must be at index 0, 1, or 2. This gives us only 3 cases to try. For each case, calculate x = nums2[0] - nums1[first_kept_index], then greedily match remaining elements. Return the minimum valid x found. TutorialsPoint - Find the Integer Added to Array II | Three-Case Enumeration Approach
Asked in
Google 15 Amazon 12 Microsoft 8
8.4K Views
Medium Frequency
~18 min Avg. Time
156 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen