Hackerrank - Sequence Equation Solution
1 min read

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 .


  • , 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

2 3 1

Sample Output 0


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

4 3 5 1 2

Sample Output 1


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):
d2 = {v:k for k,v in d1.items()}
for x,y in sorted(d1.items(), key=lambda k_v:k_v[1]):

Enjoying these posts? Subscribe for more

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

That's okay. But without advertising-income, we can't keep making this site awesome.

We don't have any banner, Flash, animation, obnoxious sound, or popup ad. We do not implement these annoying types of ads!

We need money to operate the site, and almost all of it comes from our online advertising.

Please add thepoorcoder.com to your ad blocking whitelist or disable your adblocking software.