Hackerrank - Sequence Equation Solution

Hackerrank - Sequence Equation Solution

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

Solution in Python

from collections import defaultdict

d1= defaultdict(int)
n = int(input())
a = list(map(int,input().split()))
for x,y in enumerate(a,1):
d1[x]=y
d2 = {v:k for k,v in d1.items()}
for x,y in sorted(d1.items(), key=lambda k_v:k_v[1]):
print(d2[x])