blob: 260014cfd1e335a230fb5d611e5300f2177a0573 (
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
|
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw{ say postderef signatures };
no warnings qw{ experimental::postderef experimental::signatures };
my @L = ( -25, -10, -7, -3, 2, 4, 8, 10 );
my $target = 0;
# a + b + c = target
# a <= b <= c
# in this case, we're given an array that's pre-sorted, so we roll
# with that.
for my $i ( 0 .. scalar @L - 1 ) {
for my $j ( $i .. scalar @L - 1 ) {
for my $k ( $j .. scalar @L - 1 ) {
# sort has made me stop using $a and $b as vars
# but I'm working with indexes anyway
my $l = $L[$i] + $L[$j] + $L[$k];
next unless $l == $target;
say qq{ $L[$i] + $L[$j] + $L[$k] = $l };
}
}
}
|