# Sequence Equation – HackerRank Solution

In this post, we will solve Sequence Equation HackerRank Solution. This problem (Sequence Equation) is a part of HackerRank Algorithms series.

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, that is x increments from 1 to n, find any integer y such that p(p(y)) = x and keep a history of the values of y in a return array.

Example

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

Each value of x between 1 and 5, the length of the sequence, is analyzed as follows:

1. x = 1 = p[3],p[4] = 3, so p[p[4]] = 1
2. x = 2 = p[2],p[2] = 2, so p[p[2]] = 2
3. x = 3 = p[4],p[5] = 4, so p[p[5]] = 3
4. x = 4 = p[5],p[1] = 5, so p[p[1]] = 4
5. x = 5 = p[1],p[3] = 1, 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.

permutationEquation has the following parameter(s):

• int p[n]: an array of integers

Returns

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

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

## Constraints

• 1 <= n <= 50
• 1 <= p[i] <= 50, where 1 <= i <= n.
• 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 p(1) = 2p(2) = 3, and p(3) = 1, we calculate and print the following values for each x from 1 to n:

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

Sample Input 1

``````5
4 3 5 1 2``````

Sample Output 1

``````1
3
5
4
2``````

## Solution – Sequence Equation – HackerRank Solution

### C++

```#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
typedef pair<int, int> II;

int main() {
#ifdef LOCAL
freopen("Data.inp", "r", stdin);
freopen("Data.out", "w", stdout);
#endif

int n, a[100];
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];

for (int i = 1; i <= n; ++i) {
for (int j = 1; j <= n; ++j) if (a[a[j]] == i) {
cout << j << endl;
break;
}
}

return 0;
}
```

### Python

```#!/usr/bin/env python3

import sys

def permutationEquation(p):
output = []

for num in range(1, max(p)+1):
output.append(p.index(p.index(num)+1)+1)

return output

if __name__ == "__main__":
n = int(input().strip())
p = list(map(int, input().strip().split(' ')))
result = permutationEquation(p)
print ("\n".join(map(str, result)))

```

### Java

```import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {
public static void main(String args[] ) throws Exception {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[]=new int[n];
for(int i = 0 ;i<n;i++)
arr[i]=sc.nextInt();
for(int i = 0 ;i<n;i++)
{
int pos = 0;
for(int j = 0 ; j<n;j++)
{
if(arr[j]==i+1)
{
pos = j+1 ;
break;
}
}
int pos1=0;
for(int j = 0 ; j<n;j++)
{
if(arr[j]==pos)
{
pos1 = j ;break;
}
}
System.out.println(pos1+1);
}
}
}
```

Note: This problem (Sequence Equation) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Education and Learning purpose.