-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
70 lines (54 loc) · 1.38 KB
/
index.php
File metadata and controls
70 lines (54 loc) · 1.38 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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
require('vendor/autoload.php');
$A = [-1, -3, 6, 4, 0, 1, 2, 7];
function solutionA($A)
{
// no values, 1 is the answer
if (
!is_array($A) ||
empty($A)
) {
return 1;
}
// get only positive values
$positiveValues = array_filter($A, function ($item) {
return $item > 0;
});
// we have no positive values in the array, the answer is 1
if (empty($positiveValues)) {
return 1;
}
// get unique values
$uniquePositiveValues = array_unique($positiveValues);
// sorted in ascending order
// also resets the keys
sort($uniquePositiveValues);
// the lowest number is greater than 1, the answer is 1
if (min($uniquePositiveValues) > 1) {
return 1;
}
foreach ($uniquePositiveValues as $key => $value) {
// compare our key + 1, against the value, if it doesn't match we've got a missing value
$check = $key + 1;
if ($check != $value) {
return $check;
}
}
// we made it this far, we don't have any gaps
// the answer is the count of our values + 1
return count($uniquePositiveValues) + 1;
}
function solutionB($A)
{
$target = range(1, 1000000);
$diff = array_diff($target, $A);
return reset($diff);
}
$resA = solutionA($A);
echo $resA;
echo "\n";
echo "\n";
$resB = solutionB($A);
echo $resB;
echo "\n";
echo "\n";