-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPicking Numbers.php
48 lines (39 loc) · 1.22 KB
/
Picking Numbers.php
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
<?php
/*
QUESTION:
Given an array of integers, find the longest subarray where the absolute difference between any two elements is less than or equal to 1.
Example
a = [1,1,2,2,4,4,5,5,5]
There are two subarrays meeting the criterion: [1,1,2,2] and [4,4,5,5,5]. The maximum length subarray has 5 elements.
Function Description
Complete the pickingNumbers function in the editor below.
pickingNumbers has the following parameter(s):
- int a[n]: an array of integers
Returns
- int: the length of the longest subarray that meets the criterion
*/
function pickingNumbers($a) {
// Write your code here
sort($a);
$counter = 0;
$temp = [];
for($i = 0; $i < count($a)-1; $i++){
$value = $a[$i];
if(!array_key_exists($value, $temp))
{
$temp[$value] = [];
array_push($temp[$value], $value);
for($j = $i+1; $j < count($a); $j++){
if($a[$i] == $a[$j] || ($a[$i]+1) == $a[$j]){
array_push($temp[$value], $a[$j]);
}
}
}
$count = count($temp[$value]);
$counter = $count > $counter ? $count : $counter;
}
return $counter;
}
$a = [1,1,2,2,4,4,5,5,5];
echo pickingNumbers($a);
?>