> 
> Hi, ALL,
> 
>       I thought there should be a solution/algorithm already 
> to this problem and would like to check here first. 
> 
> 
> The problem: To return all the pairs (one number from each 
> array, one number can only be in one pair) that have the 
> smallest sum of absolute differences from two arrays.
> 
> For example 
> 
> my @xa=(8765,6000,4765,3000,1530,1500,1465,1234,1000);
> my @xb=(8675,6000,4567,3100,3000,1545,1515,1485,1324,1005);
> 

Why not put them in a hash and loop over the key/value pairs?

my %nums = (
        3000 => 3005,
        2000 => 4678
);

foreack $k(keys %nums) {

        if($k > $nums{$k}) {
                my $diff = $k - $nums{$k};
        }       
        else {
                my $diff = $nums{$k} - $k;
        }
        print "Diff : $diff N1 :$k N2 : $nums{$k}";
}

> to return:
> diff  num_1   num_2
> 0     3000    3000
> 0     6000    6000
> 5     1000    1005
> 15    1500    1515
> 15    1530    1545
> 20    1465    1485
> -90   8765    8675
> 90    1234    1324
> -198  4765    4567
> .     .       3100
> 
>       Notice that |1500-1485|=|1500-1515|.

Is that a problem?

> 
> 
>       Many thanks!
> 
> 
>       Nengbing
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to