Product of Two Run-Length Encoded Arrays - Problem

Run-length encoding is a compression algorithm that allows for an integer array nums with many segments of consecutive repeated numbers to be represented by a (generally smaller) 2D array encoded. Each encoded[i] = [vali, freqi] describes the ith segment of repeated numbers in nums where vali is the value that is repeated freqi times.

For example, nums = [1,1,1,2,2,2,2,2] is represented by the run-length encoded array encoded = [[1,3],[2,5]]. Another way to read this is "three 1's followed by five 2's".

The product of two run-length encoded arrays encoded1 and encoded2 can be calculated using the following steps:

  1. Expand both encoded1 and encoded2 into the full arrays nums1 and nums2 respectively.
  2. Create a new array prodNums of length nums1.length and set prodNums[i] = nums1[i] * nums2[i].
  3. Compress prodNums into a run-length encoded array and return it.

You are given two run-length encoded arrays encoded1 and encoded2 representing full arrays nums1 and nums2 respectively. Both nums1 and nums2 have the same length. Each encoded1[i] = [vali, freqi] describes the ith segment of nums1, and each encoded2[j] = [valj, freqj] describes the jth segment of nums2.

Return the product of encoded1 and encoded2.

Note: Compression should be done such that the run-length encoded array has the minimum possible length.

Input & Output

Example 1 — Basic Case
$ Input: encoded1 = [[1,3],[2,2]], encoded2 = [[6,2],[3,3]]
Output: [[6,2],[6,1],[6,2]]
💡 Note: Expanded arrays are [1,1,1,2,2] and [6,6,3,3,3]. Products: [6,6,3,6,6]. Compressed: [[6,2],[3,1],[6,2]]
Example 2 — Single Segments
$ Input: encoded1 = [[1,2]], encoded2 = [[4,2]]
Output: [[4,2]]
💡 Note: Both arrays have single segments. Products: [1×4, 1×4] = [4,4]. Compressed: [[4,2]]
Example 3 — Different Lengths
$ Input: encoded1 = [[2,1],[3,1],[4,1]], encoded2 = [[5,3]]
Output: [[10,1],[15,1],[20,1]]
💡 Note: First array [2,3,4], second array [5,5,5]. Products: [10,15,20]. All different, so [[10,1],[15,1],[20,1]]

Constraints

  • 1 ≤ encoded1.length, encoded2.length ≤ 105
  • encoded1[i].length == 2
  • 1 ≤ encoded1[i][1], encoded2[i][1] ≤ 104
  • -105 ≤ encoded1[i][0], encoded2[i][0] ≤ 105

Visualization

Tap to expand
Product of Two Run-Length Encoded Arraysencoded1 = [[1,3],[2,2]]represents: [1,1,1,2,2]encoded2 = [[6,2],[3,3]]represents: [6,6,3,3,3]Array 1 Segments[1×3] [2×2]Element-wise Multiplication1×6, 1×6, 1×3, 2×3, 2×3Array 2 Segments[6×2] [3×3]Result: [[6,2],[3,1],[6,2]]Compressed: [6,6,3,6,6]🎯 Key Insight: Process segments directly without full expansion
Understanding the Visualization
1
Input
Two run-length encoded arrays representing compressed sequences
2
Process
Multiply corresponding elements without full expansion
3
Output
Return compressed product array
Key Takeaway
🎯 Key Insight: Use two pointers to process overlapping segments without expanding the full arrays
Asked in
Facebook 15 Google 12 Amazon 8
23.4K Views
Medium Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen