Task 1: Missing Integers
"Finding the Gaps: Identifying Missing Range Members!"
Given an array of n integers, identify all integers in the range 1..n that are not present in the array.
The Strategy: First, determine the size
n of the array. Store the elements of the array in a set or hash for O(1) lookup. Then, iterate through the numbers from 1 to n and collect those that are not present in the set.
Perl Implementation
sub missing_integers {
my (@ints) = @_;
my $n = scalar @ints;
my %present;
$present{$_} = 1 for @ints;
return [ grep { !$present{$_} } 1 .. $n ];
}
Python Implementation
def missing_integers(ints: list[int]) -> list[int]:
n = len(ints)
present = set(ints)
return [i for i in range(1, n + 1) if i not in present]