Logo
Grokking the Coding Interview: Patterns for Coding Questions
Ask Author
Back to course home

0% completed

Solution: Merge Similar Items

Problem Statement

Given two 2D arrays of item-value pairs, named items1 and items2.

  • item[i] = [value<sub>i</sub>, weight<sub>i</sub>], where each value<sub>i</sub> in these arrays is unique within its own array and is paired with a weight<sub>i</sub>.

Combine these arrays such that if an item appears in both, its values are summed up. The final merged array should be sorted based on the value<sub>i</sub>.

Examples

  1. Example 1:
    • Input: items1 = [[1,2],[4,3]], items2 = [[2,1],[4,3],[3,4]]
    • Expected Output: [[1,2],[2,1],[3,4],[4,6]]

.....

.....

.....

Like the course? Get enrolled and start learning!