-
Notifications
You must be signed in to change notification settings - Fork 0
/
largest-permutation.php
53 lines (37 loc) · 1.05 KB
/
largest-permutation.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
<?php
/*
* Complete the 'largestPermutation' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts following parameters:
* 1. INTEGER k
* 2. INTEGER_ARRAY arr
*/
function largestPermutation($k, $arr) {
$l = count($arr);
$rev = array_flip($arr);
for ($x = 0; $x < $l && $k > 0; $x++) {
if ($arr[$x] == $l - $x) {
continue;
}
$ax = $arr[$x];
$rx = $rev[$ax];
$rl = $rev[$l - $x];
$al = $arr[$rl];
$arr[$x] = $l - $x;
$arr[$rl] = $ax;
$rev[$ax] = $rl;
$rev[$al] = $rx;
$k--;
}
return $arr;
}
$fptr = fopen(getenv("OUTPUT_PATH"), "w");
$first_multiple_input = explode(' ', rtrim(fgets(STDIN)));
$n = intval($first_multiple_input[0]);
$k = intval($first_multiple_input[1]);
$arr_temp = rtrim(fgets(STDIN));
$arr = array_map('intval', preg_split('/ /', $arr_temp, -1, PREG_SPLIT_NO_EMPTY));
$result = largestPermutation($k, $arr);
fwrite($fptr, implode(" ", $result) . "\n");
fclose($fptr);