Skip to content

ORIGINAL CODING QUESTION

Run-compressed glyph length

Compress each consecutive run as one glyph plus the decimal run count when the run is longer than one. Return the resulting encoded length; the glyph always occupies one symbol.

Open practice workspace

Original exercise · easy. Editorial role relevance does not imply a company has asked this question.

Input and output

n, then n integer values. Print one integer as described.

Constraints

1 ≤ n ≤ 30; each glyph is an integer in [0,25]. Equal integers mean equal characters.

Example 1

Input

5 0 0 1 2 2

Output

5

Encoded run lengths are 2,1,2; total length 5.

Example 2

Input

4 0 0 0 0

Output

2

One glyph plus count 4 has length 2.

Explain your approach

Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?

Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.

Hint 1

Only consecutive equal glyphs share a run.

Hint 2

A singleton omits its count.

Hint 3

Longer runs add the number of decimal count digits.

Practice this question

Save your work privately, explain decisions and return to your revision queue.

Open practice workspaceBrowse the question library →