aboutsummaryrefslogtreecommitdiff
path: root/challenge-201/0rir/raku/ch-1.raku
blob: ffa33fdee2f293ed2c08d4a459b62ce4bc7ba10d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env raku
# :vim ft=raku sw=4 expandtab  # 🦋 ∅ ≡ ∩ ≢ ∈ «␤»
use v6.d;
use Test;

=begin comment
201-1: Missing Numbers          Submitted by: Mohammad S Anwar
Given an array of unique numbers, find all missing numbers in the
range 0..$n where $n is the array size.

Example 1
Input: @array = (0,1,3)
Output: 2

The array size i.e. total element count is 3, so the range is 0..3.
The missing number is 2 in the given array.
=end comment

my @Test =
    [0,]    => [1,],
    [0,1]   => [2,],
    [1,2]   => [0,],
    [0,2]   => [1,],
    [-1,-2] => [0,1,2],
    [-1,3]  => [0,1,2],
    [3,4]   => [0,1,2],
;

sub missing-numbers( @a -->Array) {
    @ = [0..@a.elems].grep: * != (grep ( -1 < * ≤  @a.elems),@a).any;
}

plan +@Test;

for @Test -> (:key(@in), :value(@exp)) {
        is missing-numbers( @in), @exp, "@in[]";
}
done-testing;

my @array = pick 5, -3..8;
say @array.sort;
say "Input: \@array = @array[]\nOutput: ", missing-numbers( @array);