Sequence Equation

Given a sequence of  integers,  where each element is distinct and satisfies . For each where , find any integer  such that  and print the value of  on a new line.
For example, assume the sequence . Each value of  between  and , the length of the sequence, is analyzed as follows:
  1. , so 
  2. , so 
  3. , so 
  4. , so 
  5. , so 
The values for  are .
Function Description
Complete the permutationEquation function in the editor below. It should return an array of integers that represent the values of .
permutationEquation has the following parameter(s):
  • p: an array of integers
Input Format
The first line contains an integer , the number of elements in the sequence.
The second line contains  space-separated integers  where .
Constraints
  • , where .
  • Each element in the sequence is distinct.
Output Format
For each  from  to , print an integer denoting any valid  satisfying the equation  on a new line.
Sample Input 0
3
2 3 1
Sample Output 0
2
3
1
Explanation 0
Given the values of , and , we calculate and print the following values for each  from  to :
  1. , so we print the value of  on a new line.
  2. , so we print the value of  on a new line.
  3. , so we print the value of  on a new line.
Sample Input 1
5
4 3 5 1 2
Sample Output 1
1
3
5
4
2
 php
<?php
// Complete the permutationEquation function below.
function permutationEquation($p) {
$arr = [];
for($i = 1; $i <= count($p); $i++) {
$key = array_search($i, $p) + 1;
$arr[] = array_search($key, $p) +1;
}
return $arr;
}
$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);

Comments

Popular posts from this blog

Intro to Tutorial Challenges

Strong Password