Camel
Peter
Peter Campbell Smith

Vowels and minima

Weekly challenge 319 — 28 April 2025

Week 319: 28 Apr 2025

Task 2

Task — Minimum common

You are given two arrays of integers. Write a script to return the minimum integer common to both arrays. If none found return -1.

Examples


Example 1
Input: @array_1 = (1, 2, 3, 4)
       @array_2 = (3, 4, 5, 6)
Output: 3
The common integer in both arrays: 3, 4
The minimum is 3.

Example 2
Input: @array_1 = (1, 2, 3)
       @array_2 = (2, 4)
Output: 2

Example 3
Input: @array_1 = (1, 2, 3, 4)
       @array_2 = (5, 6, 7, 8)
Output: -1

Analysis

My solution to this is as follows:

  • sort @array1
  • set $exists{$i} for every $i in @array2
  • find the first element $i of @array1 where $exists{$i}

There may be more efficient ways of doing it, but with a million random elements in each array this runs in under 10 seconds on my quite slow machine.

It also works if the arrays are different lengths, or if they contain negative numbers.

Try it 

Try running the script with any input:



example: 1, 3, 5, 7, 9



example: 6, 5, 4, 3, 2

Script


#!/usr/bin/perl

# Blog: http://ccgi.campbellsmiths.force9.co.uk/challenge

use v5.26;    # The Weekly Challenge - 2025-04-28
use utf8;     # Week 319 - task 2 - Minimum common
use warnings; # Peter Campbell Smith
binmode STDOUT, ':utf8';
use Encode;

minimum_common([1, 2, 3, 4], [3, 4, 5, 6]);
minimum_common([1, 2, 3, 4], [5, 6, 7, 8]);
minimum_common([1, 2, 3, 4], [3, 4, 5, 6]);
minimum_common([-1, -2, -3, 0, 1], [-7, 1, 7]);

my (@array1, @array2);
push @array1, int(rand(500) + 1) for 0 .. 99;
push @array2, int(rand(500) + 1) for 0 .. 99;
minimum_common(\@array1, \@array2);

sub minimum_common {
    
    my (@array1, @array2, %exists, $i);
    
    # initialise
    @array1 = @{$_[0]};
    @array2 = @{$_[1]};
    say qq[\nInput:  \@array1 = (] . join(', ', @array1) . '),';    
    say qq[        \@array2 = (] . join(', ', @array2) . ')';   
    
    # sort @array1 and record what $exists in @array2
    @array1 = sort {$a <=> $b} @array1;
    $exists{$_} = 1 for @array2;
    
    # look for the smallest number in both arrays
    for $i (@array1) {
        if ($exists{$i}) {
            say qq[Output: $i];
            return;
        }
    }
    say qq[Output: -1];
}

Output


Input:  @array1 = (1, 2, 3, 4),
        @array2 = (3, 4, 5, 6)
Output: 3

Input:  @array1 = (1, 2, 3, 4),
        @array2 = (5, 6, 7, 8)
Output: -1

Input:  @array1 = (1, 2, 3, 4),
        @array2 = (3, 4, 5, 6)
Output: 3

Input:  @array1 = (-1, -2, -3, 0, 1),
        @array2 = (-7, 1, 7)
Output: 1

Input:  @array1 = (53, 441, 156, 322, 200, 301, 368, 215,
   385, 365, 207, 73, 333, 180, 254, 447, 45, 332, 224,
   127, 426, 43, 215, 332, 96, 201, 104, 380, 111, 285,
   285, 340, 373, 58, 235, 363, 164, 494, 10, 184, 188,
   174, 16, 309, 424, 399, 266, 235, 318, 272, 492, 53,
   49, 461, 385, 64, 354, 72, 480, 244, 209, 478, 323,
   112, 432, 487, 397, 183, 398, 194, 14, 455, 139, 303,
   188, 245, 26, 427, 102, 53, 262, 373, 174, 231, 415,
   130, 266, 316, 183, 108, 11, 122, 227, 99, 90, 331, 58,
   500, 51, 148),
        @array2 = (228, 227, 88, 53, 411, 478, 381, 282,
   93, 292, 401, 258, 43, 102, 106, 171, 221, 224, 493,
   127, 59, 451, 69, 292, 274, 156, 224, 301, 457, 82,
   145, 40, 300, 335, 172, 346, 76, 23, 34, 266, 445, 73,
   253, 108, 221, 73, 87, 235, 335, 488, 359, 42, 39, 95,
   119, 294, 241, 183, 54, 165, 175, 13, 456, 59, 133,
   328, 210, 441, 363, 457, 21, 260, 155, 336, 427, 488,
   489, 320, 213, 435, 462, 362, 84, 176, 209, 452, 72,
   18, 203, 423, 482, 341, 447, 372, 281, 480, 216, 224,
   227, 490)
Output: 43

 

Any content of this website which has been created by Peter Campbell Smith is in the public domain