# HackerRank Sequence Equation Solution

Hello Programmers, In this post, you will learn how to solve HackerRank Sequence Equation Solution. This problem is a part of the HackerRank Algorithms Series.

One more thing to add, don’t straight away look for the solutions, first try to solve the problems by yourself. If you find any difficulty after trying several times, then look for the solutions. We are going to solve the HackerRank Algorithms problems using C, CPP, JAVA, PYTHON, JavaScript & SCALA Programming Languages.

You can practice and submit all HackerRank problem solutions in one place. Find a solution for other domains and Sub-domain. I.e. Hacker Rank solution for HackerRank C ProgrammingHackerRank C++ ProgrammingHackerRank Java Programming, HackerRank Python ProgrammingHackerRank Linux ShellHackerRank SQL Programming, and HackerRank 10 days of Javascript.

As you already know that this site does not contain only the Hacker Rank solutions here, you can also find the solution for other problems. I.e. Web Technology, Data StructuresRDBMS ProgramsJava Programs Solutions,  Fiverr Skills Test answersGoogle Course AnswersLinkedin Assessment, and Coursera Quiz Answers.

## HackerRank Sequence Equation Rotation

Given a sequence of n integers, p(1), p(2), . . . , p(n) where each element is distinct and satisfies 1 <= p(x) <= n. For eachx 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 spaceseparated 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) = 2, p(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```

## HackerRank Sequence Equation Solution

### Sequence Equation Solution in C

```#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main() {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
int i,n,a[100],j;
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
for(j=1;j<=n;j++)
{
for(i=1;i<=n;i++)
if(a[a[i]]==j)
break;
printf("%d\n",i);
}
return 0;
}```

### Sequence Equation Solution in Cpp

```#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main()
{
#ifdef ONPC
freopen("a.in", "r", stdin);
//freopen("a.out", "w", stdout);
#else
//freopen("a.in", "r", stdin);
//freopen("a.out", "w", stdout);
#endif
ios::sync_with_stdio(0);
int n;
cin >> n;
vector <int> p(n + 1);
for (int i = 1; i <= n; i++)
{
cin >> p[i];
}
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
if (p[p[j]] == i)
{
cout << j << endl;
break;
}
}
}
}```

### Sequence Equation Solution in Java

```import java.io.*;
import java.util.*;

class Main {
PrintWriter out;

void main() {
int n = in.nextInt();
int nxt[] = new int[n];
for (int i = 0; i < n; ++i) {
int y = in.nextInt();
nxt[--y] = i;
}
for (int i = 0; i < n; ++i)
out.println(nxt[nxt[i]] + 1);
}

public static void main(String[] args) {
new Main();
}

public Main() {
in = new InputReader(System.getProperty("ONLINE_JUDGE") != null ? null : "main.inp");
out = new PrintWriter(System.out);
main();
out.close();
}

StringTokenizer st = null;

try {
bf = new BufferedReader(filename == null
);
} catch (IOException e) {
e.printStackTrace();
}
}

String nextString() {
try {
while (st == null || !st.hasMoreTokens())
} catch (IOException e) {
throw new RuntimeException(e);
}
return st.nextToken();
}

long nextLong() {return Long.parseLong(nextString());}
int nextInt() {return Integer.parseInt(nextString());}
double nextDouble() {return Double.parseDouble(nextString());}
}

}```

### Sequence Equation Solution in Python

```rr = raw_input
rrM = lambda: map(int, rr().split())
N = int(rr())
A = rrM()
A = [x-1 for x in A]
B = [0] * N
for i,u in enumerate(A):
B[i] = A[u]
C = [0]*N
for i,u in enumerate(B):
C[u] = i+1
for x in C:
print x```

### Sequence Equation Solution using JavaScript

```function processData(input) {
var lines = input.split("\n");
var data = lines[1].split(' ').map(Number);
var all = {};
for(var i=0;i<data.length;i++){
all[data[i]] = i+1;
}
for(var i=1;i<=data.length;i++){
console.log(all[all[i]]);
}
}

process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});

process.stdin.on("end", function () {
processData(_input);
});```

### Sequence Equation Solution in Scala

```// main method in "Solution" will be run as your answer
object Solution {

def main(args: Array[String]) {
//Enter your code here. Read input from STDIN. Print output to STDOUT
val revP = px.zip(1 to n).toMap
(1 to n) map(revP) map(revP) foreach(println)
}

}```

### Sequence Equation Solution in Pascal

```VAR i,n,j,l:integer; a:array[1..50] of integer;
Begin
for i:=1 to n do
Begin
end;
for i:=1 to n do
Begin
for j:=1 to n do
Begin
if i = a[j] then Begin
for l:=1 to n do
Begin
if a[l] = j then Writeln(l);
end;
end;
end;
end;
END.```

Disclaimer: This problem (Sequence Equation) is generated by HackerRank but the solution is provided by Chase2learn. This tutorial is only for Educational and Learning purposes.

## FAQ:

1. How do you solve the first question in HackerRank?

If you want to solve the first question of Hackerrank then you have to decide which programing language you want to practice i.e C programming, Cpp Programing, or Java programming then you have to start with the first program HELLO WORLD.

2. How do I find my HackerRank ID?

You will receive an email from HackerRank to confirm your access to the ID. Once you have confirmed your email, the entry will show up as verified on the settings page. You will also have an option to “Make primary”. Click on that option. Read more

3. Does HackerRank detect cheating?

yes, HackerRank uses a powerful tool to detect plagiarism in the candidates’ submitted code. The Test report of a candidate highlights any plagiarized portions in the submitted code and helps evaluators to verify the integrity of answers provided in the Test.

4. Does HackerRank use camera?

No for coding practice Hackerrank does not use camera but for companies’ interviews code submission time Hackerrank uses the camera.

5. Should I put HackerRank certificate on resume?

These certificates are useless, and you should not put them on your resume. The experience you gained from getting them is not useless. Use it to build a portfolio, and link to it on your resume.

6. Can I retake HackerRank test?

The company which sent you the HackerRank Test invite owns your Test submissions and results. It’s their discretion to permit a reattempt for a particular Test. If you wish to retake the test, we recommend that you contact the concerned recruiter who invited you to the Test and request a re-invite.

7. What is HackerRank?

HackerRank is a tech company that focuses on competitive programming challenges for both consumers and businesses. Developers compete by writing programs according to provided specifications. Wikipedi

Finally, we are now, in the end, I just want to conclude some important message for you

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.