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.
Examples# Example 1# 1
2
3
4
5
6
7
8
Input: nums1 = [ 2 , 6 , 4 ], nums2 = [ 9 , 7 , 5 ]
Output: 3
Explanation:
The integer added to each element of `nums1` is 3.
Example 2# 1
2
3
4
5
6
7
8
Input: nums1 = [ 10 ], nums2 = [ 5 ]
Output: - 5
Explanation:
The integer added to each element of `nums1` is - 5.
Example 3# 1
2
3
4
5
6
7
8
Input: nums1 = [ 1 , 1 , 1 , 1 ], nums2 = [ 1 , 1 , 1 , 1 ]
Output: 0
Explanation:
The integer added to each element of `nums1` is 0.
Constraints# 1 <= nums1.length == nums2.length <= 1000 <= nums1[i], nums2[i] <= 1000The test cases are generated in a way that there is an integer x such that nums1 can become equal to nums2 by adding x to each element of nums1. Solution# Method 1 – Difference of Sums# Intuition# If every element in nums1 is increased by x to get nums2, then the difference between the sum of nums2 and the sum of nums1, divided by the length, gives x.
Approach# Compute the sum of nums1 and nums2. The answer x is (sum(nums2) - sum(nums1)) // n, where n is the length of the arrays. Return x. Code#
Cpp
Go
Java
Kotlin
Python
Rust
Typescript
1
2
3
4
5
6
7
8
class Solution {
public :
int findTheIntegerAddedToArray(vector< int >& nums1, vector< int >& nums2) {
int s1 = accumulate(nums1.begin(), nums1.end(), 0 );
int s2 = accumulate(nums2.begin(), nums2.end(), 0 );
return (s2 - s1) / nums1.size();
}
};
1
2
3
4
5
6
func findTheIntegerAddedToArray (nums1 , nums2 []int ) int {
s1 , s2 := 0 , 0
for _ , v := range nums1 { s1 += v }
for _ , v := range nums2 { s2 += v }
return (s2 - s1 ) / len(nums1 )
}
1
2
3
4
5
6
7
8
class Solution {
public int findTheIntegerAddedToArray (int [] nums1, int [] nums2) {
int s1 = 0, s2 = 0;
for (int v : nums1) s1 += v;
for (int v : nums2) s2 += v;
return (s2 - s1) / nums1.length ;
}
}
1
2
3
4
5
6
7
class Solution {
fun findTheIntegerAddedToArray (nums1: IntArray, nums2: IntArray): Int {
val s1 = nums1.sum()
val s2 = nums2.sum()
return (s2 - s1) / nums1.size
}
}
1
2
3
class Solution :
def findTheIntegerAddedToArray (self, nums1: list[int], nums2: list[int]) -> int:
return (sum(nums2) - sum(nums1)) // len(nums1)
1
2
3
4
5
impl Solution {
pub fn find_the_integer_added_to_array (nums1: Vec< i32 > , nums2: Vec< i32 > ) -> i32 {
(nums2.iter().sum::< i32 > () - nums1.iter().sum::< i32 > ()) / nums1.len() as i32
}
}
1
2
3
4
5
6
7
class Solution {
findTheIntegerAddedToArray (nums1 : number [], nums2 : number []): number {
const s1 = nums1 .reduce ((a , b ) => a + b , 0 );
const s2 = nums2 .reduce ((a , b ) => a + b , 0 );
return (s2 - s1 ) / nums1 .length ;
}
}
Complexity# ⏰ Time complexity: O(n), where n is the length of the arrays, as we sum both arrays. 🧺 Space complexity: O(1), as only a few variables are used.