Find the Integer Added to Array I (LeetCode)
Problem Descriptionβ
Problem Statement | Solution Link | LeetCode Profile |
---|---|---|
Find the Integer Added to Array I | Find the Integer Added to Array I Solution on LeetCode | vaishu_1904 |
Problem Descriptionβ
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
.
Example 1β
- Input:
nums1 = [2, 6, 4], nums2 = [9, 7, 5]
- Output:
3
- Explanation: The integer added to each element of
nums1
is3
.
Example 2β
- Input:
nums1 = [10], nums2 = [5]
- Output:
-5
- Explanation: The integer added to each element of
nums1
is-5
.
Example 3β
- Input:
nums1 = [1, 1, 1, 1], nums2 = [1, 1, 1, 1]
- Output:
0
- Explanation: The integer added to each element of
nums1
is0
.
Constraintsβ
1 <= nums1.length == nums2.length <= 100
0 <= nums1[i], nums2[i] <= 1000
- The test cases are generated in a way that there is an integer
x
such thatnums1
can become equal tonums2
by addingx
to each element ofnums1
.
Approachβ
To find the integer x
that was added to each element of nums1
to obtain nums2
, we can use the following approach:
- Iterate through the arrays
nums1
andnums2
. - Calculate the difference
x
for the first pair of elements fromnums1
andnums2
. - Ensure that the calculated
x
is consistent for all elements.
Solution Codeβ
Pythonβ
def find_integer_added_to_array(nums1, nums2):
if len(nums1) != len(nums2):
return None # Inconsistent input lengths
x = nums2[0] - nums1[0]
for i in range(1, len(nums1)):
if nums2[i] - nums1[i] != x:
return None # Inconsistent difference found
return x
C++β
#include <vector>
#include <iostream>
using namespace std;
class Solution {
public:
int findIntegerAddedToArray(vector<int>& nums1, vector<int>& nums2) {
if (nums1.size() != nums2.size()) {
return INT_MIN; // Inconsistent input lengths
}
int x = nums2[0] - nums1[0];
for (size_t i = 1; i < nums1.size(); ++i) {
if (nums2[i] - nums1[i] != x) {
return INT_MIN; // Inconsistent difference found
}
}
return x;
}
};
Javaβ
class Solution {
public Integer findIntegerAddedToArray(int[] nums1, int[] nums2) {
if (nums1.length != nums2.length) {
return null; // Inconsistent input lengths
}
int x = nums2[0] - nums1[0];
for (int i = 1; i < nums1.length; i++) {
if (nums2[i] - nums1[i] != x) {
return null; // Inconsistent difference found
}
}
return x;
}
Conclusionβ
The above solutions determine the integer x that was added to each element of nums1 to obtain nums2. By ensuring consistency across the entire array, we validate the difference and return the result. Adjustments for different languages and edge cases ensure robustness across various inputs.