Inspired by this Stack Overflow post.
Given an ascending-sorted array of possibly duplicated integers, your goal is to increment each number by a counter, starting at 0, that resets for each group.
Spec:
- Any numbers may be negative (but if so, they'll be at the beginning, because the array is sorted),
- The array will have at least one element,
- There may be any number of integers in one group
- The groups of numbers have nothing to do with one another
To demonstrate:
[1, 1, 1, 1, 10, 10, 20, 20, 20, 30, 40, 40, 40, 40]
should become this
[1, 2, 3, 4, 10, 11, 20, 21, 22, 30, 40, 41, 42, 43]
because
1 1 1 1 10 10 20 20 20 30 40 40 40 40
+ 0 1 2 3 0 1 0 1 2 0 0 1 2 3
------- ----- -------- -- -----------
1 2 3 4 10 11 20 21 22 30 40 41 42 43
Test cases
input -> output
[1, 2, 3] -> [1, 2, 3]
[1, 1, 2, 2, 3, 3] -> [1, 2, 2, 3, 3, 4]
[0, 0, 0, 0, 0] -> [0, 1, 2, 3, 4]
[1, 1, 10, 10, 100, 100, 100, 100] -> [1, 2, 10, 11, 100, 101, 102, 103]
[-5, -5, -5, -5, -4, 4, 4, 4, 4, 9, 9] -> [-5, -4, -3, -2, -4, 4, 5, 6, 7, 9, 10]
[1, 1, 1, 1, 2, 2] -> [1, 2, 3, 4, 2, 3]
[1, 1, 2, 2, 1, 1]
? What would the output for that be,[1, 2, 2, 3, 1, 2]
or[1, 2, 2, 3, 3, 4]
? \$\endgroup\$[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0, 1, 0, 1]
to it, to get[10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 19, 20, 20, 21]
\$\endgroup\$