-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCounting Sort 1.php
52 lines (39 loc) · 1.44 KB
/
Counting Sort 1.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
<?php
/**
* @author: syed ashraf ullah
* date: 26/04/2020
* problem: https://www.hackerrank.com/challenges/countingsort1/problem
*/
// Complete the countingSort function below.
function countingSort($arr) {
// define an array which size is 100 and initial value is zero
$result = array_fill (0,100,0);
// For each array value increment the value of result array
// Example: for value 3 increment the $result[3]++;
foreach ($arr as $value) {
$result[$value]++;
}
return $result;
}
/**
* Smaple input #1
*/
// $arr = array(1,1,3,2,1);
/**
* Smaple input #2
*/
// $arr = array(63, 54, 17, 78, 43, 70, 32, 97, 16, 94, 74, 18, 60, 61, 35, 83, 13, 56, 75, 52, 70, 12, 24, 37, 17, 0, 16, 64, 34, 81, 82, 24, 69, 2, 30, 61, 83, 37, 97, 16, 70, 53, 0, 61, 12, 17, 97, 67, 33, 30, 49, 70, 11, 40, 67, 94, 84, 60, 35, 58, 19, 81, 16, 14, 68, 46, 42, 81, 75, 87, 13, 84, 33, 34, 14, 96, 7, 59, 17, 98, 79, 47, 71, 75, 8, 27, 73, 66, 64, 12, 29, 35, 80, 78, 80, 6, 5, 24, 49, 82);
// $result = countingSort($arr);
// foreach($result as $value) {
// echo $value.' ';
// }
// 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 = countingSort($arr);
fwrite($fptr, implode(" ", $result) . "\n");
fclose($stdin);
fclose($fptr);