diff options
| author | Fung Cheok Yin <61836418+E7-87-83@users.noreply.github.com> | 2020-03-09 00:22:59 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-03-09 00:22:59 +0800 |
| commit | 47b7ca54c0994eb431ffaa8b9a3504a343f3c985 (patch) | |
| tree | 0eb0249ef0169939dc3ee561536429b9037ccfb1 /challenge-050 | |
| parent | 919a838ca5659099ca26ea7756a9b7df1e481948 (diff) | |
| download | perlweeklychallenge-club-47b7ca54c0994eb431ffaa8b9a3504a343f3c985.tar.gz perlweeklychallenge-club-47b7ca54c0994eb431ffaa8b9a3504a343f3c985.tar.bz2 perlweeklychallenge-club-47b7ca54c0994eb431ffaa8b9a3504a343f3c985.zip | |
Create ch-2.pl
Diffstat (limited to 'challenge-050')
| -rw-r--r-- | challenge-050/cheok-yin-fung/perl/ch-2.pl | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/challenge-050/cheok-yin-fung/perl/ch-2.pl b/challenge-050/cheok-yin-fung/perl/ch-2.pl new file mode 100644 index 0000000000..b060dcc7da --- /dev/null +++ b/challenge-050/cheok-yin-fung/perl/ch-2.pl @@ -0,0 +1,33 @@ +#!/usr/bin/perl +use strict; + +# Caution: This script is valid only when the list of integers DISTINCT. + +# Proposition: There can only be 1 noble integer. +# Proof by Contradiction: +# Let r be the length of the list, +# k be the firstly found noble integer +# and j be the secondly found noble integer +# (by this algorithm, the first found noble integer will be the smallest noble integer;) +# (by the DISTINCT assumption, k<j #(a) +# numbers of integers larger than k: r - position(k) +# numbers of integers larger than j: r - position(j) , +# while r-position(j) < r-position(k), #(b) +# since position(j) > position(k) after sorting. +# By the nobleness condition +# k == r-position(k) #(c) +# j == r-position(j) #(d) +# Substituting (c) & (d) into (a) implies r-position(k) < r-position(j) , while this inequality violates (b) !!! QED + +my @L = @ARGV; #usage: perl ch-2.pl 2 6 1 3 + + +@L = sort {$a <=> $b} @L; + + +my $i=0; +while ($i<=$#L) { + if ($L[$i] == $#L-$i ) {print $L[$i]; exit;} + $i++; +} +print "-1"; |
