For this challenge, we will define the "complement" of a list of integers A
as being any list B
such that the union of A
and B
is a list of consecutive integers with no repeats.
In other words, B
is the complement of A
if
B
has all of the integers between the minimum and maximum ofA
which are missing fromA
, andB
has none of the integers present inA
For example: [2, 4, 6, 7]
is a complement of [1, 3, 5, 8, 9]
, [1, 3, 5]
, and [3, 5]
, but it is not a complement of [1, 2, 3, 5]
or [1, 3, 5, 9]
.
Worth noting: For a list of consecutive integers, a complement may be the empty list, as no integers are "missing" between the minimum and maximum.
Any given list of integers with no repeats has infinitely many complements (proof left as an exercise for the reader), but has exactly one smallest complement, which itself has this very same property.
For example, if we start with the list [1, 2, 3, 5, 8, 13]
and repeatedly find the smallest complement, we get this chain of lists:
[1, 2, 3, 5, 8, 13]
[4, 6, 7, 9, 10, 11, 12]
[5, 8]
[6, 7]
[]
The smallest complement of the empty list is the empty list, so we can stop here.
Challenge
Given a list of non-repeating positive integers A
, output the A
's smallest complement, followed by that list's smallest complement, all the way until the empty list.
Rules
- You may choose to include or omit the input list in your output.
- You may choose to include or omit the empty list in your output.
- The input list will always have complements. (i.e. the list will never contain duplicates)
- The input will be sorted.
- The input will never be the empty list.
- The input will never contain 0 or negative integers. (although they are well defined)
- This is code-golf, so shortest code in bytes wins.
Examples
Outputs will include both the original list and the empty list, for clarity
input => output
[1, 3, 5, 7, 9] => [[1, 3, 5, 7, 9], [2, 4, 6, 8], [3, 5, 7], [4, 6], [5], []]
[5, 10, 15, 20] => [[5, 10, 15, 20], [6, 7, 8, 9, 11, 12, 13, 14, 16, 17, 18, 19], [10, 15], [11, 12, 13, 14], []]
[5, 10, 15] => [[5, 10, 15], [6, 7, 8, 9, 11, 12, 13, 14], [10], []]
[1, 2, 6, 7, 11, 12] => [[1, 2, 6, 7, 11, 12], [3, 4, 5, 8, 9, 10], [6, 7], []]
[1, 2, 3] => [[1, 2, 3], []]
[5, 6, 7] => [[5, 6, 7], []]