Find the Integer Added to Array I - Problem

You are given two arrays of equal length, nums1 and nums2.

Each element in nums1 has been increased (or decreased in the case of negative) by an integer, represented by the variable 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 integer x.

Input & Output

Example 1 — Basic Case
$ Input: nums1 = [2,6,3], nums2 = [5,9,6]
Output: 3
💡 Note: Each element in nums1 increased by 3: [2+3, 6+3, 3+3] = [5,9,6] = nums2
Example 2 — Negative Adjustment
$ Input: nums1 = [10,6,2], nums2 = [5,1,-3]
Output: -5
💡 Note: Each element in nums1 decreased by 5: [10-5, 6-5, 2-5] = [5,1,-3] = nums2
Example 3 — Zero Adjustment
$ Input: nums1 = [1,1,1,1], nums2 = [1,1,1,1]
Output: 0
💡 Note: Arrays are already equal, so x = 0: [1+0, 1+0, 1+0, 1+0] = [1,1,1,1]

Constraints

  • 1 ≤ nums1.length == nums2.length ≤ 1000
  • -1000 ≤ nums1[i], nums2[i] ≤ 1000

Visualization

Tap to expand
Find the Integer Added to Array Inums1263+ xnums2596Solution: x = nums2[0] - nums1[0]x = 5 - 2 = 3Works for any corresponding pair!Answer: x = 3
Understanding the Visualization
1
Input Arrays
Two arrays: nums1 and nums2 of equal length
2
Transformation
nums1[i] + x = nums2[i] for all i
3
Calculate X
x = nums2[i] - nums1[i] for any i
Key Takeaway
🎯 Key Insight: Since all elements are adjusted by the same constant, any element pair reveals the value of x
Asked in
Google 15 Amazon 12 Microsoft 8
25.0K Views
Medium Frequency
~5 min Avg. Time
892 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