aboutsummaryrefslogtreecommitdiff
path: root/challenge-198/dave-jacoby/perl/ch-1.pl
blob: 412300592e234f22b2fcbe9487a5a9cb354b7f8c (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
#!/usr/bin/env perl

use strict;
use warnings;
use experimental qw{ fc say postderef signatures state };

my @examples = ( [ 1, 0, 3, 0, 0, 5 ], [ 2, 5, 8, 1 ], [3] );

for my $e (@examples) {
    my @list = $e->@*;
    my $out  = max_gap(@list);
    my $list = join ', ', @list;
    say <<"END";
    Input:  \@list = ($list)
    Output: $out
END
}

sub max_gap( @list ) {
    return 0 if scalar @list < 2;
    @list = sort @list;
    my %hash;
    for my $i ( 1 .. -1 + scalar @list ) {
        my $gap = abs $list[$i] - $list[ $i - 1 ];
        $hash{$gap}++;
    }
    my ($max) = sort { $b <=> $a } keys %hash;
    return $hash{$max};
}