-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmin_min_max.php
More file actions
35 lines (31 loc) · 1.31 KB
/
min_min_max.php
File metadata and controls
35 lines (31 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
// 7 kyu - MinMinMax
// Given an unsorted array of integers, find the smallest number in the array, the largest number in the array, and the smallest number between the two array bounds that is not in the array.
//
// For instance, given the array [-1, 4, 5, -23, 24], the smallest number is -23, the largest number is 24, and the smallest number between the array bounds is -22. You may assume the input is well-formed.
//
// You solution should return an array [smallest, minimumAbsent, largest]
//
// The smallest integer should be the integer from the array with the lowest value.
//
// The largest integer should be the integer from the array with the highest value.
//
// The minimumAbsent is the smallest number between the largest and the smallest number that is not in the array.
//
// minMinMax([-1, 4, 5, -23, 24]); //[-23, -22, 24]
// minMinMax([1, 3, -3, -2, 8, -1]); //[-3, 0, 8]
// minMinMax([2, -4, 8, -5, 9, 7]); //[-5, -3,9]
function minMinMax($array) {
$min_absent = min(array_diff(range(min($array),max($array)),$array));
return array(min($array), $min_absent, max($array));
}
// Alternative solution:
// function minMinMax($arr){
// $max = max($arr);
// $min = min($arr);
// $minAbsent = $min;
// while(in_array($minAbsent, $arr)) $minAbsent += 1;
//
// return [$min, $minAbsent, $max];
// }
?>