HackerRank Sequence Equation Solution

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

HackerRank Sequence Equation Solution
HackerRank Sequence Equation Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

HackerRank Sequence Equation Solution

Task

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 {
	InputReader in;
	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();
	}
	class InputReader {
		BufferedReader bf;
		StringTokenizer st = null;
		
		InputReader(String filename) {
			try {
				bf = new BufferedReader(filename == null
					? new InputStreamReader(System.in)
					: new FileReader(filename)
				);
			} catch (IOException e) {
				e.printStackTrace();
				System.err.println("use stdin instead");
				bf = new BufferedReader(new InputStreamReader(System.in));
			}
		}
		
		String nextString() {
			try {
				while (st == null || !st.hasMoreTokens())
					st = new StringTokenizer(bf.readLine());
			} 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());}
	}
}
Ezoicreport this ad

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 n = io.StdIn.readLine().toInt
    val px = io.StdIn.readLine().split(" ").toList.map(_.toInt)
    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
Readln(n);
for i:=1 to n do
Begin
Read(a[i]);
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;
Readln;
END.

Disclaimer: This problem (Sequence Equation) is generated by HackerRank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning purposes.

Next: HackerRank Taum and Bday Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad