Kids who are learning how to count often know runs of numbers, but can't seem to put those runs together properly.
For example, they might say:
1,2,3,4,7,8,9,10
Sometimes kids will realize that they skipped some numbers, and go back:
1,2,3,4,7,8,5,6,7,8,9,10
This is clearly the superior pattern. We need to identify them.
To identify these lists:
We identify the minimum
M
and the maximumN
of the listWe step through the list. If the current number is greater than or equal to any member of the list to its right, then we remove the current number.
Iff the remaining list contains all numbers from
M
toN
, then we return a truthy value.
You can assume your input list will contain at least 1 element. You can assume that all integers will be non-negative.
Test cases:
Truthy:
0
10
0 0 0
1 0 1
0 1 2 3 4 5 6 7 8 9 10
0 1 2 3 0 1 2 3
0 1 2 3 4 5 5
0 1 1 2 2 3
0 3 6 1 4 7 2 5 8 3 4 5 6 7 8
1 3 5 7 2 3 4 5 6 7
5 6 0 1 2 3 6 7 4 5 6 7
5 6 7 8
5 5 6 7 8
4 6 7 8 3 4 5 6 7 8
Falsy:
1 0
4 3 2 1
1 2 3 7 8 9
0 1 2 3 1 3
0 1 2 3 1 3 4
0 1 2 3 1 3 2 4
0 1 2 3 1 3 2 4 3
1 3 5 7 2 4 6 8
0 1 2 1 3 4 5 6
4 5 6 3 4 5
This is code-golf, so make your answers as short as possible!
1
later down the line). You'd also remove every other element (except for the last 1), so you'd end up with0 1
, which is not0 1 2 3 4 5
\$\endgroup\$