44
5
Given a nonempty array of positive integers, "increment" it once as follows:
If all the array elements are equal, append a
1
to the end of the array. For example:[1] -> [1, 1] [2] -> [2, 1] [1, 1] -> [1, 1, 1] [3, 3, 3, 3, 3] -> [3, 3, 3, 3, 3, 1]
Else, increment the first element in the array that is the array's minimum value. For example:
[1, 2] -> [2, 2] [2, 1] -> [2, 2] [3, 1, 1] -> [3, 2, 1] -> [3, 2, 2] -> [3, 3, 2] -> [3, 3, 3] [3, 4, 9, 3] -> [4, 4, 9, 3] -> [4, 4, 9, 4] -> [5, 4, 9, 4] -> [5, 5, 9, 4] -> ...
(Each ->
represents one increment, which is all your program needs to do.)
Output the resulting incremented array.
The shortest code in bytes wins.
Does 0 count as positive integer – Downgoat – 2016-11-28T23:14:46.807
20@Downgoat 0 is not ever positive on PPCG. If 0 was allowed, the term would be "non-negative" – ETHproductions – 2016-11-28T23:23:02.270