#K83332. Compress Consecutive List Elements

    ID: 36174 Type: Default 1000ms 256MiB

Compress Consecutive List Elements

Compress Consecutive List Elements

You are given a list of integers. Your task is to compress the list by replacing consecutive identical elements with a tuple where the first element is the value and the second is the frequency of its consecutive occurrences.

More formally, given a list \( lst = [a_1, a_2, \dots, a_n] \), you need to produce a new list \( result \) where each block of identical consecutive numbers \( a_i, a_{i+1}, \dots, a_{i+k} \) is replaced by \( (a_i, k+1) \). For example, if \( lst = [4,4,4,5,5,2,2,2,2] \), then \( result = [(4,3), (5,2), (2,4)] \).

inputFormat

The first line contains an integer \( n \) (which can be zero) representing the number of elements in the list. The second line contains \( n \) space-separated integers.

outputFormat

Output the compressed list in the following format: a list of tuples. Each tuple is represented as (element, count). The entire output should be enclosed in square brackets, and tuples should be separated by a comma and a space.

For example: [(4, 3), (5, 2), (2, 4)]

## sample
9
4 4 4 5 5 2 2 2 2
[(4, 3), (5, 2), (2, 4)]