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 Arrays INPUT encoded1: [1,3] [2,2] Expands to nums1: 1 1 1 2 2 encoded2: [6,2] [3,3] Expands to nums2: 6 6 3 3 3 Both arrays have length 5 Same total frequency ALGORITHM STEPS 1 Two Pointers i for encoded1, j for encoded2 2 Process Segments min(freq1, freq2) elements 3 Multiply Values product = val1 * val2 4 Merge Adjacent Combine same values Processing Steps: 1*6=6, min(3,2)=2 --> [6,2] 1*3=3, min(1,3)=1 --> [3,1] 2*3=6, min(2,2)=2 --> [6,2] No merge: 6,3,6 all different O(n+m) time, O(1) extra space FINAL RESULT Product (element-wise): 6 6 3 6 6 RLE Encoded Output: [6,2] [3,1] [6,2] OUTPUT: [[6,2],[3,1],[6,2]] Verification: 2+1+2 = 5 elements Matches input length OK Key Insight: Use two pointers to process both RLE arrays simultaneously without expanding. At each step, compute product for min(freq1, freq2) elements and subtract from both. Merge consecutive segments with same product value for minimum output length. TutorialsPoint - Product of Two Run-Length Encoded Arrays | Optimal Solution
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