#K49507. Run-Length Encoding
Run-Length Encoding
Run-Length Encoding
Given an integer \(n\) representing the number of bytes and a list of \(n\) integer values (each between 0 and 255), compress the list using Run-Length Encoding (RLE). In RLE, consecutive occurrences of the same byte are replaced by the byte followed by the count of its repetition.
For example, if the input list is [10, 10, 10, 20, 20, 20, 20, 20, 30, 30]
, then the compressed output should be [10, 3, 20, 5, 30, 2]
.
The transformation can be mathematically described as follows:
[ \text{If } A = [a_1, a_2, \dots, a_n], \text{ then the output is } [v_1, c_1, v_2, c_2, \dots, v_k, c_k], ]
where each pair \((v_i, c_i)\) represents the value and its consecutive count.
inputFormat
The input will be provided via standard input (stdin) and consists of two lines:
- The first line contains an integer \(n\), the number of bytes.
- The second line contains \(n\) space-separated integers representing the byte values.
If \(n = 0\), the second line will be empty.
outputFormat
Output the run-length encoded sequence as a single line of space-separated integers via standard output (stdout). There should be no extra spaces at the beginning or end of the line.
## sample10
10 10 10 20 20 20 20 20 30 30
10 3 20 5 30 2