Decompress Run-Length Encoded List - Problem

We are given a list nums of integers representing a list compressed with run-length encoding.

Consider each adjacent pair of elements [freq, val] = [nums[2*i], nums[2*i+1]] (with i >= 0). For each such pair, there are freq elements with value val concatenated in a sublist. Concatenate all the sublists from left to right to generate the decompressed list.

Return the decompressed list.

Input & Output

Example 1 — Basic Case
$ Input: nums = [1,2,3,4]
Output: [2,4,4,4]
💡 Note: First pair [1,2]: frequency 1, value 2 → add 2 once. Second pair [3,4]: frequency 3, value 4 → add 4 three times. Result: [2,4,4,4]
Example 2 — Multiple Pairs
$ Input: nums = [1,1,2,3]
Output: [1,3,3]
💡 Note: First pair [1,1]: frequency 1, value 1 → add 1 once. Second pair [2,3]: frequency 2, value 3 → add 3 twice. Result: [1,3,3]
Example 3 — Zero Frequency
$ Input: nums = [0,1,2,2]
Output: [2,2]
💡 Note: First pair [0,1]: frequency 0, value 1 → add nothing. Second pair [2,2]: frequency 2, value 2 → add 2 twice. Result: [2,2]

Constraints

  • 2 ≤ nums.length ≤ 100
  • nums.length % 2 == 0
  • 1 ≤ nums[2 * i] ≤ 100
  • 1 ≤ nums[2 * i + 1] ≤ 100

Visualization

Tap to expand
Run-Length Decoding: Expand Compressed PairsInput Array:1234freq=1, val=2freq=3, val=4Expansion:21 copy4443 copiesFinal Result:2444[2,4,4,4] - Decompressed Array
Understanding the Visualization
1
Input
Array with alternating frequency-value pairs: [1,2,3,4]
2
Process
Expand each pair: [1,2]→[2], [3,4]→[4,4,4]
3
Output
Concatenated result: [2,4,4,4]
Key Takeaway
🎯 Key Insight: Each pair [frequency, value] tells us exactly how many times to repeat a value in the output
Asked in
Google 15 Amazon 12 Facebook 8 Microsoft 6
25.0K Views
Medium Frequency
~10 min Avg. Time
850 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