#3131
Find the Integer Added to Array I
EasyArrayHash MapArray
Approaches
Brute ForceOptimal
Complexity Comparison
| Brute Force | Optimal Solution★ | |
|---|---|---|
| Time | O(n²) | O(n) |
| Space | O(1) | O(1) |
💡
Intuition
Time O(n)Space O(1)
The optimal solution leverages the relationship between the minimum values of the two arrays. By finding the difference between the minimum values, we can directly compute x.
⚙️
Algorithm
3 steps- 1Step 1: Find the minimum value of nums1 and nums2.
- 2Step 2: Calculate x as the difference between the minimum of nums2 and the minimum of nums1.
- 3Step 3: Return x.
solution.py5 lines
1# Full working Python code
2
3def find_added_integer(nums1, nums2):
4 return min(nums2) - min(nums1)
5ℹ
Complexity note: The time complexity is O(n) because we only need to traverse each array once to find the minimum values. The space complexity is O(1) since we are using a constant amount of extra space.
- 1The difference between the minimum values of the two arrays directly gives the integer x.
- 2Sorting both arrays is unnecessary for finding x, as we only need the minimum values.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.