Skip to main content
LeetCode 2570, Easy. Topics: Array, Hash Table, Two Pointers. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 16 parametrized pytest cases, and a playground notebook:

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

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, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026