use strict; use warnings; my @orig = (1..10); my @altered = ( [qw( 1 2 3 4 9 5 6 7 8 10)], [qw( 2 3 4 1 5 6 7 8 9 10)], [qw( 1 2 3 4 10 5 6 7 8 9)], [qw( 1 2 3 5 4 6 7 8 9 10)], [qw( 1 3 4 5 6 7 2 8 9 10)], [qw( 1 2 3 4 5 6 7 8 10 9)], [qw( 1 2 3 4 6 5 7 8 9 10)], ); for my $test (@altered) { my %count = (); my ($val, $flag) = (undef, $orig[0] == $test->[0]); for my $i (0..$#orig) { # keep track of what the nums in each array add up to $count{before} += $orig[$i]; $count{after} += $test->[$i]; if (($flag && ($count{before} != $count{after})) || (!$flag && ($count{before} == $count{after}))) { $val = ( $test->[$i] == $orig[$i+1]) ? $orig[$i] : $test->[$i]; last; } } print "$val moved between (@orig) and (@$test)\n"; } #### The sum of a sequence of integers from x to x+n equals ((x+n)(x+n+1) - (x-1)(x))/2