Min-Min-Max
August 12, 2016
Our straight forward solution calls library functions to get the minimum and maximum, then increments the minimum until it finds a number not in the input:
(define (min-min-max xs) (let loop ((m (+ (apply min xs) 1))) (if (member m xs) (loop (+ m 1)) (values (apply min xs) m (apply max xs)))))
> (min-min-max '(-1 4 5 -23 24)) -23 -22 24
We’ll ignore the redundant calculation of the minimum.
Our second solution inserts each integer into a distinct priority queue (so we can ignore the problem of duplicates), then pops the priority queue for the minimum, then pops the priority queue repeatedly until it finds two non-consecutive integers, and finally pops the priority queue until it is exhausted to find the maximum:
(define (min-min-max xs) (let loop ((xs xs) (dpq (make-dpq <))) (if (pair? xs) (loop (cdr xs) (dpq-insert (car xs) dpq)) (let ((m1 (dpq-first dpq)) (prev (dpq-first dpq))) (let loop ((dpq (dpq-rest dpq))) (if (= (+ prev 1) (dpq-first dpq)) (loop (dpq-rest dpq)) (let ((m2 (+ prev 1))) (let loop ((prev (dpq-first dpq)) (dpq (dpq-rest dpq))) (if (dpq-empty? dpq) (values m1 m2 prev) (loop (dpq-first dpq) (dpq-rest dpq)))))))))))
> (min-min-max '(-1 4 5 -23 24)) -23 -22 24
I’m not sure that’s better. The original is O(n) if you implement member
as an O(1) operation, but the “improved” version is O(n log n). At least we were able to use the distinct priority queue of the previous exercise.
You can run the program at http://ideone.com/aa6FIq.
I think my naive perl queue version is similar to your second solution…
Here’s a solution in Java.
The first solution sorts the array, getting min and max from the ends, and looping over the array to find the smallest number between the two array bounds that is not in the array.
The second solution is not particularly creative. It loops over the array, finding the min and max elements, and creating a hash set of array elements. Then it loops over a range from min to max to find the smallest number between the two array bounds that is not in the array.
Output:
Sorry, should be this:
My last solution was not particularly creative. Here’s a bash script that solves the problem.
In my last post, my code was mistakenly reading in the data already sorted. Here’s the corrected version.
Output:
My two cents in D…
Output:
[-23, -22, 24]
Powershell…
#build array
$array = @(-1, 4, 5, -23, 24)
#sort array
$sarray = $array | sort
#first element of array – smallest
$sarray[0]
#use array count to find last element of array – largest
$sarrayc = $sarray.Count – 1
#last element of array
$sarray[$sarrayc]
#use for loop to count up from smallest number
for($i = $sarray[0];$i -le $sarray[$sarrayc];$i++)
{
#loop over each number is sorted array
foreach($num in $sarray)
{
#if the count equals a number in the array break
if($i -eq $num)
{
break
}
}
#if the counter doesn’t equal a number in the array break
if($i -ne $num)
{
#print the counter #
write-host $i
break
}
}
JAVASCRIPT:
Three solutions in Racket. The straightforward one uses sorting, the creative one uses a hash table like (almost) everybody else, and the other creative one uses a priority queue.