↓Skip to main content
  1. LeetCode/

LeetCode 2570: Merge Two 2D Arrays by Summing Values

·2 mins· ·
LeetCode Easy Two-Pointers Array
Wei Yi Chung
Author
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
Table of Contents

Meta Data
#

Difficulty: easy First Attempt: 2026-01-31

  • Submissions: Accepted (first try)

Approach
#

Both arrays are sorted by id with unique ids in each. It is merging two sorted sequences (like LC 88), except matching ids add their values:

  • Same id: output [id, sum of both values] and advance both pointers.
  • Different: output the smaller id and advance that pointer.
  • Once one side runs out, append the rest of the other.

My Solution
#

class Solution:
    def mergeArrays(self, nums1: List[List[int]], nums2: List[List[int]]) -> List[List[int]]:
        i = j = 0
        res = []
        while i < len(nums1) and j < len(nums2):
            if nums1[i][0] == nums2[j][0]:
                res.append([nums1[i][0], nums1[i][1] + nums2[j][1]])
                i += 1
                j += 1
            elif nums1[i][0] < nums2[j][0]:
                res.append(nums1[i])
                i += 1
            else:
                res.append(nums2[j])
                j += 1
        return res + nums1[i:] + nums2[j:]

My original handled “one side is exhausted” inside the same loop (while i < n or j < m with three cases inside). Looping only while both sides have elements and appending the rest afterwards is shorter.

Complexity
#

Time O(n + m), space O(n + m).

Mistakes to Avoid
#

  • Collecting into a dict and sorting: it works, but ignores that the inputs are sorted and adds O(k log k).
  • Forgetting to append the leftovers after the loop.

Interview Explanation
#

Both arrays are sorted by id, so I merge with two pointers: equal ids add their values and advance both; otherwise I output the smaller id and advance it. When one side runs out, I append the rest of the other. Time O(n + m).

Related

LeetCode 2540: Minimum Common Value
·2 mins
LeetCode Easy Two-Pointers Array
LeetCode 2540 note: one pointer per sorted array, advance the smaller side, and the first match is the answer
LeetCode 2200: Find All K-Distant Indices in an Array
·2 mins
LeetCode Easy Two-Pointers Array
LeetCode 2200 note: extend each key k positions both ways, and keep a pointer to how far output has gone so overlapping ranges are not emitted twice
LeetCode 3069: Distribute Elements Into Two Arrays I
·1 min
LeetCode Easy Array Two-Pointers Simulation
LeetCode 3069 note: simulate the rule: the first two go to separate arrays, then each element joins the array whose last element is larger
LeetCode 942: DI String Match
·1 min
LeetCode Easy Array Two-Pointers String Greedy
LeetCode 942 note: two pointers hold the smallest and largest unused values; place the smallest on I and the largest on D
LeetCode 349: Intersection of Two Arrays
·1 min
LeetCode Easy Array Hash-Table Two-Pointers Binary-Search
LeetCode 349 note: convert both arrays to sets and intersect them
LeetCode 350: Intersection of Two Arrays II
·1 min
LeetCode Easy Array Hash-Table Two-Pointers Binary-Search
LeetCode 350 note: an intersection keeping multiplicity, where Counter’s & takes the smaller count from each side