The Bear's Den

Enter at your own risk

Subtracting Pairs

Task 1: Min Gap

Submitted by: Mohammad Sajid Anwar


You are given an array of integers, @ints, increasing order.

Write a script to return the element before which you find the smallest gap.

Example 1

Input: @ints = (2, 8, 10, 11, 15)
Output: 11

 8 - 2  => 6
10 - 8  => 2
11 - 10 => 1
15 - 11 => 4

11 is where we found the min gap.

Example 2

Input: @ints = (1, 5, 6, 7, 14)
Output: 6

 5 - 1 => 4
 6 - 5 => 1
 7 - 6 => 1
14 - 7 => 7

6 and 7 where we found the min gap, so we pick the first instance.

Example 3

Input: @ints = (8, 20, 25, 28)
Output: 28

 8 - 20 => 14
25 - 20 => 5
28 - 25 => 3

28 is where we found the min gap.

Solution

We take the differences between all pairs of consecutive numbers, find the index of the (first) minimum and pick the integer from the next index.

use strict;
use warnings;
use PDL;
use PDL::NiceSlice;

sub min_gap {
    my $ints = long \@_;
    $ints(($ints(1:-1) - $ints(0:-2))->minimum_ind + 1;-);
}

See the full solution to task 1.

Task 2: Min Diff

Submitted by: Mohammad Sajid Anwar


You are given an array of integers, @ints.

Write a script to find the minimum difference between any two elements.

Example 1

Input: @ints = (1, 5, 8, 9)
Output: 1

1, 5 => 5 - 1 => 4
1, 8 => 8 - 1 => 7
1, 9 => 9 - 1 => 8
5, 8 => 8 - 5 => 3
5, 9 => 9 - 5 => 4
8, 9 => 9 - 8 => 1

Example 2

Input: @ints = (9, 4, 1, 7)
Output: 2

9, 4 => 9 - 4 => 5
9, 1 => 9 - 1 => 8
9, 7 => 9 - 7 => 2
4, 1 => 4 - 1 => 3
4, 7 => 7 - 4 => 3
1, 7 => 7 - 1 => 6

Solution

We calculate the difference between all number pairs as a matrix, set the matrix’ upper right triangle to BAD and find the minimum over the absolute values from the remaining elements. Actually it would be sufficient to set the matrix’ diagonal to BAD such that an element is never paired with itself.

use strict;
use warnings;
use PDL;
use PDL::NiceSlice;

sub min_diff {
    my $ints = long \@_;
    my $diff = $ints - $ints(*1);
    $diff += zeroes($diff)->setvaltobad(0)->tricpy;

    $diff->abs->min;
}

See the full solution to task 2.


If you have a question about this post or if you like to comment on it, feel free to open an issue in my github repository.