diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2020-04-19 14:39:52 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-04-19 14:39:52 +0100 |
| commit | a9e8656cebafa6115f4530c1a8811cdca02fb388 (patch) | |
| tree | bb4bf139225a92f7327472783c43853414083484 | |
| parent | bca45afd829c9f69c71507c53375ca97d91a8364 (diff) | |
| parent | 94b7f911d5a610a9b6c2e2eca6fcc4bff12cf39a (diff) | |
| download | perlweeklychallenge-club-a9e8656cebafa6115f4530c1a8811cdca02fb388.tar.gz perlweeklychallenge-club-a9e8656cebafa6115f4530c1a8811cdca02fb388.tar.bz2 perlweeklychallenge-club-a9e8656cebafa6115f4530c1a8811cdca02fb388.zip | |
Merge pull request #1595 from wanderdoc/master
Task #1.
| -rw-r--r-- | challenge-056/wanderdoc/perl/ch-1.pl | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/challenge-056/wanderdoc/perl/ch-1.pl b/challenge-056/wanderdoc/perl/ch-1.pl new file mode 100644 index 0000000000..b446b6e76d --- /dev/null +++ b/challenge-056/wanderdoc/perl/ch-1.pl @@ -0,0 +1,28 @@ +#!perl +use strict; +use warnings FATAL => qw(all); + +=prompt +You are given an array @N of positive integers (sorted) and another non negative integer k. +Write a script to find if there exists 2 indices i and j such that A[i] - A[j] = k and i != j. +It should print the pairs of indices, if any such pairs exist. +Example: + @N = (2, 7, 9) + $k = 2 +Output : 2,1 + +=cut + + +my @N = (0 .. 9); # (2, 7, 9); +my $k = shift || 2; + +for my $j ( 0 .. $#N ) +{ + for my $i ( $j .. $#N ) + { + next if $i == $j; + print join(", ", $i, $j), $/ if $k == $N[$i] - $N[$j]; + + } +}
\ No newline at end of file |
