> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Merge Two 2D Arrays by Summing Values

> Tested Python solution for LeetCode 2570 with 16 pytest cases. Generate a practice environment with lcpy.

LeetCode 2570, [Easy](/catalog/easy). Topics: [Array](/catalog/topics/array), [Hash Table](/catalog/topics/hash-table), [Two Pointers](/catalog/topics/two-pointers). [View on LeetCode](https://leetcode.com/problems/merge-two-2d-arrays-by-summing-values/description/).

Generate this problem as a practice environment: tested reference solution, 16 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 2570   # by problem number
lcpy gen -s merge_two_2d_arrays_by_summing_values   # by problem name
```

## Problem

You are given two \<strong>2D\</strong> integer arrays \<code>nums1\</code> and \<code>nums2\</code>.

* \<code>nums1\[i] = \[id\<sub>i\</sub>, val\<sub>i\</sub>]\</code> indicate that the number with the id \<code>id\<sub>i\</sub>\</code> has a value equal to \<code>val\<sub>i\</sub>\</code>.
* \<code>nums2\[i] = \[id\<sub>i\</sub>, val\<sub>i\</sub>]\</code> indicate that the number with the id \<code>id\<sub>i\</sub>\</code> has a value equal to \<code>val\<sub>i\</sub>\</code>.

Each array contains \<strong>unique\</strong> ids and is sorted in \<strong>ascending\</strong> order by id.

Merge the two arrays into one array that is sorted in ascending order by id, respecting the following conditions:

* Only ids that appear in at least one of the two arrays should be included in the resulting array.
* Each id should be included \<strong>only once\</strong> and its value should be the sum of the values of this id in the two arrays. If the id does not exist in one of the two arrays, then assume its value in that array to be \<code>0\</code>.

Return \<em>the resulting array\</em>. The returned array must be sorted in ascending order by id.

### Examples

```
Input: nums1 = [[1,2],[2,3],[4,5]], nums2 = [[1,4],[3,2],[4,1]]
Output: [[1,6],[2,3],[3,2],[4,6]]
Explanation: The resulting array contains the following:
- id = 1, the value of this id is 2 + 4 = 6.
- id = 2, the value of this id is 3.
- id = 3, the value of this id is 2.
- id = 4, the value of this id is 5 + 1 = 6.
```

```
Input: nums1 = [[2,4],[3,6],[5,5]], nums2 = [[1,3],[4,3]]
Output: [[1,3],[2,4],[3,6],[4,3],[5,5]]
Explanation: There are no common ids, so we just include each id with its value in the resulting list.
```

### Constraints

* 1 \<= nums1.length, nums2.length \<= 200
* nums1\[i].length == nums2\[j].length == 2
* 1 \<= id\<sub>i\</sub>, val\<sub>i\</sub> \<= 1000
* Both arrays contain unique ids.
* Both arrays are in strictly ascending order by id.

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/merge_two_2d_arrays_by_summing_values/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/merge_two_2d_arrays_by_summing_values/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(m + n)
    # Space: O(m + n) for the output
    def merge_arrays(self, nums1: list[list[int]], nums2: list[list[int]]) -> list[list[int]]:
        result: list[list[int]] = []
        i = 0
        j = 0
        while i < len(nums1) and j < len(nums2):
            id1, val1 = nums1[i]
            id2, val2 = nums2[j]
            if id1 == id2:
                result.append([id1, val1 + val2])
                i += 1
                j += 1
            elif id1 < id2:
                result.append([id1, val1])
                i += 1
            else:
                result.append([id2, val2])
                j += 1
        result.extend(nums1[i:])
        result.extend(nums2[j:])
        return result
```

## Complexity

| Time | Space |
| - | - |
| O(m + n) | O(m + n) for the output |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.