sub TwoSum { my( $target, @nums ) = @_; # assumption: @nums are already sorted. # caveat: the nums in the list might not be uniq!!! my %num; @num{@nums} = (); # for existence tests. for my $i2 ( reverse( 1 .. $#nums ) ) { my $d = $target - $nums[$i2]; if ( exists $num{$d} ) { # gotta find the _first_ occurrence of this number in the list: my($i1) = grep { $nums[$_] == $d } 0 .. $i2-1; return( $i1, $i2 ); } } die }