---
# System prepended metadata

title: 2570. Merge Two 2D Arrays by Summing Values
tags: [Leetcode, Two Pointers, Easy]

---

# 2570. Merge Two 2D Arrays by Summing Values
###### tags: `Leetcode` `Easy` `Two Pointers`
Link: https://leetcode.com/problems/merge-two-2d-arrays-by-summing-values/description/
## Code
```python=
class Solution:
    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:
        ans = []
        m, n = len(nums1), len(nums2)
        p1, p2 = 0, 0
        while p1<m or p2<n:
            if p1==m:
                ans.append(nums2[p2])
                p2 += 1
            elif p2==n:
                ans.append(nums1[p1])
                p1 += 1
            else:
                a, b = nums1[p1][0], nums2[p2][0]
                if a<b:
                    ans.append(nums1[p1])
                    p1 += 1
                elif a>b:
                    ans.append(nums2[p2])
                    p2 += 1
                else:
                    ans.append([nums1[p1][0], nums1[p1][1]+nums2[p2][1]])
                    p1 += 1
                    p2 += 1
        return ans
```