The Weekly Challenge 309

Min Gap and Min Diff

Task 1: Min Gap

You are given an array of integers, @ints, in 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 Gaps: 6, 2, 1, 4 — smallest is 1 before 11

Example 2

Input: @ints = (1, 5, 6, 7, 14) Output: 6 Gaps: 4, 1, 1, 7 — smallest is 1, first at 6

Example 3

Input: @ints = (8, 20, 25, 28) Output: 28 Gaps: 12, 5, 3 — smallest is 3 before 28

Logic

Iterate through consecutive pairs in the sorted array, compute gaps, and track the minimum. Return the element following the smallest gap (first occurrence on ties).

Perl Solution

ch-1.pl

#!/usr/bin/perl use strict; use warnings; use feature 'say'; sub min_gap { my ($ints_ref) = @_; return unless ref($ints_ref) eq 'ARRAY'; my $n = scalar @$ints_ref; return if $n < 2; my $min_delta = $ints_ref->[1] - $ints_ref->[0]; my $result = $ints_ref->[1]; for my $i (2 .. $n - 1) { my $diff = $ints_ref->[$i] - $ints_ref->[$i - 1]; if ($diff < $min_delta) { $min_delta = $diff; $result = $ints_ref->[$i]; } } return $result; } unless (caller) { use Test::More tests => 7; is(min_gap([2,8,10,11,15]), 11, 'Example 1'); is(min_gap([1,5,6,7,14]), 6, 'Example 2'); is(min_gap([8,20,25,28]), 28, 'Example 3'); ok(!defined(min_gap([])), 'Empty'); ok(!defined(min_gap([42])), 'Single'); is(min_gap([1,3,5,6,9]), 6, 'Tied gap'); is(min_gap([1,2,3,4]), 2, 'All same'); }

Python Solution

ch-1.py

from typing import Optional def min_gap(arr: list[int]) -> Optional[int]: if len(arr) < 2: return None min_delta = float('inf') candidate = None for i in range(1, len(arr)): diff = arr[i] - arr[i - 1] if diff < min_delta: min_delta = diff candidate = arr[i] return candidate

Task 2: Min Diff

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

Example 2

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

Logic

Sort the array first. After sorting, the minimum difference must be between two consecutive elements. Compute adjacent differences and return the minimum. O(n log n).

Perl Solution

ch-2.pl

#!/usr/bin/perl use strict; use warnings; use feature 'say'; sub min_diff { my ($ints_ref) = @_; return unless ref($ints_ref) eq 'ARRAY'; my @ints = @$ints_ref; return if scalar(@ints) < 2; my @sorted = sort { $a <=> $b } @ints; my $min_delta = $sorted[1] - $sorted[0]; for my $i (2 .. $#sorted) { my $delta = $sorted[$i] - $sorted[$i - 1]; $min_delta = $delta if $delta < $min_delta; } return $min_delta; } unless (caller) { use Test::More tests => 7; is(min_diff([1,5,8,9]), 1, 'Example 1'); is(min_diff([9,4,1,7]), 2, 'Example 2'); ok(!defined(min_diff([])), 'Empty'); ok(!defined(min_diff([42])), 'Single'); is(min_diff([3,3,5,8]), 0, 'Duplicates'); is(min_diff([-10,-5,-3,-1]), 2, 'Negatives'); is(min_diff([-5,0,3,8]), 3, 'Mixed'); }

Python Solution

ch-2.py

from typing import Optional def min_diff(arr: list[int]) -> Optional[int]: if len(arr) < 2: return None s = sorted(arr) min_d = s[1] - s[0] for i in range(2, len(s)): d = s[i] - s[i - 1] if d < min_d: min_d = d return min_d