-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSequence Equation.php
38 lines (30 loc) · 991 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
<?php
/*
QUESTION:
Given a sequence of n integers. p(1), P(2),...,p(n) where each element is distinct and
satisfies 1 <= p(T) <= n. For each x where 1 <= x <= n. that is x increments from 1 to n. find
any integer y such that p(p(Y)) === x and keep a history of the values of y in a return array.
Example
P = [5, 2, 1, 3, 4]
Each value of g between 1 and 5. the length of the sequence, is analyzed as follows:
1. x = 1 = p[3], p[4] = 3. so p[p[4]] = 1
2. x = 2 = p[2], p[2] = 2. so p[p[2]] = 2
1. x = 3 = p[4], p[5] = 4. so p[p[5]] = 3
1. x = 4 = p[5], p[1] = 5. so p[p[1]] = 4
1. x = 5 = p[1], p[3] = 1. so p[p[3]] = 5
The values for y are [4, 2, 5, 1, 3].
*/
// Solution
function permutationEquation($p) {
// Write your code here
$min = min($p);
$max = max($p);
$res = [];
for($i = $min; $i <= $max; $i++){
array_push($res, array_search((array_search($i, $p)+1), $p)+1);
}
return $res;
}
$p = [5, 2, 1, 3, 4];
print_r(permutationEquation($p));
?>