Sequence Equation ⬀
Given a sequence of n integers, p(1), p(2) where each element is distinct and satisfies 1 ≤ p(x) ≤ 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.
p = [5, 2, 1, 3, 4]
Each value of x between 1 and 5, the length of the sequence, is analyzed as follows:
x = 1 ≡ p[3],p[4] = 3, sop[p[4]] = 1x = 2 ≡ p[2],p[2] = 3, sop[p[2]] = 2x = 3 ≡ p[3],p[5] = 3, sop[p[5]] = 3x = 4 ≡ p[5],p[1] = 3, sop[p[1]] = 4x = 5 ≡ p[1],p[3] = 3, sop[p[3]] = 5
The values for y are [4, 2, 5, 1, 3].
Complete the permutationEquation function in the editor below.
permutationEquation has the following parameter(s):
int p[n]: an array of integers
int[n]: the values ofyfor allxin the arithmetic sequence1ton
The first line contains an integer n, the number of elements in the sequence.
The second line contains n space-separated integers p[i] where 1 ≤ i ≤ n.
1 ≤ n ≤ 501 ≤ p[i] ≤ 50, where1 ≤ i ≤ n.- Each element in the sequence is distinct.
3
2 3 1
2
3
1
Given the values of p(1) = 2, p(2) = 3, and p(3) = 1, we calculate and print the following values for each x from 1 to n:
x = 1 ≡ p(3) = p(p(2)) = p(p(y)), so we print the value ofy = 2on a new line.x = 2 ≡ p(1) = p(p(3)) = p(p(y)), so we print the value ofy = 3on a new line.x = 3 ≡ p(2) = p(p(1)) = p(p(y)), so we print the value ofy = 1on a new line.
5
4 3 5 1 2
1
3
5
4
2