Jump to content

Maximum difference between adjacent elements of list: Difference between revisions

No edit summary
Line 310:
<pre>The maximum difference between adjacent pairs of the list is: 7
The pairs with this difference are: (1, 8) (2, 9) (10, 3)</pre>
 
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
 
use strict;
use warnings;
use List::Util qw( reduce max );
 
my @list = (1,8,2,-3,0,1,1,-2.3,0,5.5,8,6,2,9,11,10,3);
 
my %diffs;
reduce { $diffs{ abs $a - $b } .= " $a,$b"; $b } @list;
my $max = max keys %diffs;
print "$_ ==> $max\n" for split ' ', $diffs{ $max };</lang>
{{out}}
<pre>
1,8 ==> 7
2,9 ==> 7
10,3 ==> 7
</pre>
 
=={{header|Phix}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.