- Sequence Equation

 



Given a sequence of  integers,  where each element is distinct and satisfies . For each  where , that is  increments from  to , find any integer  such that  and keep a history of the values of  in a return array.

Example

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.

permutationEquation has the following parameter(s):

  • int p[n]: an array of integers

Returns

  • int[n]: the values of  for all  in the arithmetic sequence  to 

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.

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
Program:
#!/bin/python3

import math
import os
import random
import re
import sys

#
# Complete the 'permutationEquation' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts INTEGER_ARRAY p as parameter.
#

def permutationEquation(p,n):
    list1=[]
    for i in range(1,n+1):
        x=p.index(i)
        y=p.index(x+1)
        list1.append(y+1)
    return list1
    # Write your code here

if __name__ == '__main__':
    fptr = open(os.environ['OUTPUT_PATH'], 'w')

    n = int(input().strip())

    p = list(map(intinput().rstrip().split()))

    result = permutationEquation(p,n)

    fptr.write('\n'.join(map(str, result)))
    fptr.write('\n')

    fptr.close()

Compiler Message
Success
Input (stdin)
  • 3
  • 2 3 1
Expected Output
  • 2
  • 3
  • 1

Post a Comment

Previous Post Next Post