Sequence Equation

Sequence Equation

Given a sequence of n integers, p(1),p(2),…,p(n) where each element is distinct and satisfies 1 <= p(x) <= n. For each x where 1 <= x <= n, find any integer y such that p(p(y)) = x and print the value of y on a new line.

For example, assume the sequence p = [5, 2, 1, 3, 4]. Each value of x between 1 and 5, the length of the sequence, is analyzed as follows:

  1. , so p[p[4]] = 1
  2. , so p[p[2]] = 2
  3. , so p[p[5]] = 3
  4. , so p[p[1]] = 4
  5. , so p[p[3]] = 5

The values for y are [4,2,5,1,3].

Function Description

Complete the permutationEquation function in the editor below. It should return an array of integers that represent the values of y.

permutationEquation has the following parameter(s):

  • p: an array of integers

Input Format

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 a <= i <= n.

Constraints

  • 1 <= n <= 50
  • 1 <= p[i] <= 50, where 1 <= i <= n.
  • Each element in the sequence is distinct.

Output Format

For each x from 1 to n, print an integer denoting any valid y satisfying the equation on a new line.

Sample Input 0

1
2
3
2 3 1

Sample Output 0

1
2
3
2
3
1

Explanation 0

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:

  1. , so we print the value of y = 2 on a new line.
  2. , so we print the value of y = 3 on a new line.
  3. , so we print the value of y = 1 on a new line.

Sample Input 1

1
2
5
4 3 5 1 2

Sample Output 1

1
2
3
4
5
1
3
5
4
2

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Complete the permutationEquation function below.
function permutationEquation(p) {
let values = (p || []).reduce((target, value, index) => {
target[value] = index + 1;

return target;
}, []);

return (p || []).reduce((target, value, index) => {
target.push(values[values[index + 1]])

return target;
}, []);
}
Buy Me A Coffee

Comments

10Days of JS 30Days of Code Algorithm Android Debug Bridge Android Debugging Basic for Web Blog Browsers Chrome으로 Android Debugging 방법 Correctness and the Loop Invariant hackerrank solution in javascript Debug Tools Development Environment in MacOS ES6 Front-End Funny String of Algorithms hackerrank solution in javascript Funny String of Algorithms hackerrank solution in typescript Generator Github Page with Hexo Github Pages HackerRank HackerRank in a String of Algorithms hackerrank solution in javascript HackerRank in a String of Algorithms hackerrank solution in typescript Hexo Hexo Icarus theme Hexo 블로그 만들기 Hexo+Github How Browsers work Insertion Sort - Part 1 hackerrank solution in javascript Insertion Sort - Part 2 hackerrank solution in javascript JS Library JavaScript Level1 Level2 Loops MacOS 개발 환경 설정하기 Mobile web Debugging Node.js Pangrams of Algorithms hackerrank solution in javascript Pangrams of Algorithms hackerrank solution in typescript Problem Solving Programmers Quicksort 1 - Partition hackerrank solution in javascript React RoadMap Running Time of Algorithms hackerrank solution in javascript Safari Debugging Safari Technology Preview Settings Sorting String Strings Strong Password of Algorithms hackerrank solution in javascript TypeScript blog iPhone Safari Debugging 방법 insertion sort 모바일 웹 디버깅 아이폰 사파리를 디버깅하는 방법 안드로이드 디버그 브리지 안드로이드 디버깅하는 방법
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×