-
Notifications
You must be signed in to change notification settings - Fork 0
/
Find the Median.php
70 lines (52 loc) · 1.25 KB
/
Find the Median.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
/**
* @author: syed ashraf ullah
* date: 30/04/2020
* problem: https://www.hackerrank.com/challenges/find-the-median/problem
*/
// Complete the findMedian function below.
function findMedian($arr) {
$arr = quickSort($arr);
$size = sizeof($arr);
return $arr[$size/2];
}
function quickSort($ar) {
if (sizeof($ar) <= 1 ){
return $ar;
}
$left = array();
$right = array();
$p = $ar[0];
$size = sizeof($ar);
for ($i=1; $i < $size; $i++) {
if ($p < $ar[$i]) {
$right[] = $ar[$i];
continue;
}
$left[] = $ar[$i];
}
return merge(quickSort($left),quickSort($right), $p);
}
function merge($left, $right, $p) {
$left[] = $p;
foreach ($right as $value) {
$left[] = $value;
}
return $left;
}
/**
* Sample #1
*/
// $arr = array(0, 1, 2, 4, 6, 5, 3);
// $result = findMedian($arr);
// echo $result;
// return;
$fptr = fopen(getenv("OUTPUT_PATH"), "w");
$stdin = fopen("php://stdin", "r");
fscanf($stdin, "%d\n", $n);
fscanf($stdin, "%[^\n]", $arr_temp);
$arr = array_map('intval', preg_split('/ /', $arr_temp, -1, PREG_SPLIT_NO_EMPTY));
$result = findMedian($arr);
fwrite($fptr, $result . "\n");
fclose($stdin);
fclose($fptr);