-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sequence Equation.php
62 lines (45 loc) · 995 Bytes
/
Sequence Equation.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
<?php
/**
* @author: syed ashraf ullah
* date: 20/04/2020
*/
// Complete the permutationEquation function below.
function permutationEquation($p) {
$size = sizeof($p);
$i = 0;
$result = array();
while(++$i <= $size)
{
$f = 0;
while ($p[$f] != $i){
$f++;
}
$f++;
$y = 0;
while ($p[$y] != $f){
$y++;
}
$result[$i-1] = ($y+1);
}
return $result;
}
/**
* Sample Input #1
*/
// $p = array(2, 3, 1);
/**
* Sample Input #2
*/
// $p = array(4, 3, 5, 1, 2);
// $result = permutationEquation($p);
// print_r($result);
// return;
$fptr = fopen(getenv("OUTPUT_PATH"), "w");
$stdin = fopen("php://stdin", "r");
fscanf($stdin, "%d\n", $n);
fscanf($stdin, "%[^\n]", $p_temp);
$p = array_map('intval', preg_split('/ /', $p_temp, -1, PREG_SPLIT_NO_EMPTY));
$result = permutationEquation($p);
fwrite($fptr, implode("\n", $result) . "\n");
fclose($stdin);
fclose($fptr);