Skip to content
  • Home
  • Contact Us
  • About Us
  • Privacy Policy
  • DMCA
  • Linkedin
  • Pinterest
  • Facebook
thecscience

TheCScience

TheCScience is a blog that publishes daily tutorials and guides on engineering subjects and everything that related to computer science and technology

  • Home
  • Human values
  • Microprocessor
  • Digital communication
  • Linux
  • outsystems guide
  • Toggle search form
HackerRank Sequence Equation Problem Solution

HackerRank Sequence Equation Problem Solution

Posted on April 16, 2023April 16, 2023 By Yashwant Parihar No Comments on HackerRank Sequence Equation Problem Solution

In this Post, we will solve HackerRank Sequence Equation Problem Solution.

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 a where 1 ≤ x ≤ n, that is a increments from 1 ton, 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 a 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) = 2, p(2) = 3, and p(3) = 1, we calculate and print the following values for each a from 1 ton:

  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 Problem Solution
HackerRank Sequence Equation Problem Solution

Table of Contents

  • Sequence Equation C Solution
  • Sequence Equation C++ Solution
  • Sequence Equation C Sharp Solution
  • Sequence Equation Java Solution
  • Sequence Equation Java Script Solution
  • Sequence Equation Python Solution

Sequence Equation C Solution

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

int main() {
    int n,i,j,k,flag=0;
    scanf("%d",&n);
    int *a=(int *)malloc(n*sizeof(int));
     for(i=0;i<n;i++)       
        {
           scanf("%d",&a[i]);
           //printf("\n@%d",a[i]); 
        }
      for(j=1;j<=n;j++)       
        {
          flag=0;
           for(i=0;i<n;i++)
               {
               if(a[i]==j)
               {
               for(k=0;k<n;k++)
                   {
                   if(a[k]==i+1){
                       break;
                       flag=1;
                   }
               }
               
           }
               if(flag==1)
               break;
           }
           
         printf("%d\n",k+1)   ;
        }
    return 0;
}

Sequence Equation C++ Solution

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

#define pb push_back
#define mp make_pair
#define fs first
#define se second
#define pi 2*acos(0)
#define PI 3.14159265358979323846264338

typedef long long ll;
typedef pair < int , int > pii;
typedef pair < ll , ll > pll;
const int N = 100010;

inline int in() {int x; scanf("%d",&x); return x;}
inline ll lin() {ll x; scanf("%lld",&x); return x;}

int fx[]={1,-1,0,0};
int fy[]={0,0,-1,1};

int inp[123];

int main(){
    int n = in();
    for(int i = 1; i <= n; i++){
        int x = in();
        inp[x] = i;
    }

    for(int i = 1; i <= n; i++){
        int x = inp[inp[i]];
        cout << x << '\n';
    }
}

Sequence Equation C Sharp Solution

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

namespace Solution {
class Solution {
    static void Main(string[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */
        int n = Convert.ToInt32(Console.ReadLine());
        string[] a_temp = Console.ReadLine().Split(' ');
        int[] a = Array.ConvertAll(a_temp, Int32.Parse);
        Dictionary<int, int> dic = new Dictionary<int, int>();
        for(int x = 0; x < n; x++)
            dic.Add(x + 1, a[x]);
        
        for(int x = 1; x <= n; x++)
        {
            int key1 = dic.FirstOrDefault(i => i.Value == x).Key;
            int key2 = dic.FirstOrDefault(i => i.Value == key1).Key;
            Console.WriteLine(key2);
        }
            
    }
}
}

Sequence Equation Java Solution

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

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

    public static List<Integer> permutationEquation(List<Integer> p) {
    // Write your code here
    List<Integer> result = new ArrayList<Integer>();
    List<Integer> p_inverse = new ArrayList<Integer>();
    
    for(int x = 1; x <= p.size(); x++){
        result.add(p.indexOf(p.indexOf(x) + 1) + 1);
    }
    
    
    return result;
    }

}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int n = Integer.parseInt(bufferedReader.readLine().trim());

        List<Integer> p = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
            .map(Integer::parseInt)
            .collect(toList());

        List<Integer> result = Result.permutationEquation(p);

        bufferedWriter.write(
            result.stream()
                .map(Object::toString)
                .collect(joining("\n"))
            + "\n"
        );

        bufferedReader.close();
        bufferedWriter.close();
    }
}

Sequence Equation Java Script Solution

process.stdin.resume();
process.stdin.setEncoding("ascii");
var input = "";
process.stdin.on("data", function (chunk) {
    input += chunk;
});
process.stdin.on("end", function () {
    // now we can read/parse input
    var temp = input.split('\n');
    var n = temp[0];
    var arr = temp[1];
    arr = arr.split(" ");
    var parray = [];
    for(var i=1; i<=n; i++){
        parray[i] = arr[i-1];
    }
    arr = arr.sort(function (a, b) {  return a - b;  });
    for(var i=0; i<n; i++){
        var x = arr[i];
        var temp = parray.indexOf(x);
        temp = temp.toString();
        var result = parray.indexOf(temp);
        console.log(result);
    }
});

Sequence Equation Python Solution

# Enter your code here. Read input from STDIN. Print output to STDOUT
input()
p = [0]+[int(x) for x in input().split()]

for x in range(1,len(p)):
    for y in range(1,len(p)):
        if p[p[y]] == x:
            print(y)
            break

Other Solutions

  • HackerRank Jumping on the Clouds: Revisited
  • HackerRank Find Digits Problem Solution
c, C#, C++, HackerRank Solutions, java, javascript, python Tags:C, cpp, CSharp, Hackerrank Solutions, java, javascript, python

Post navigation

Previous Post: HackerRank Circular Array Rotation Solution
Next Post: HackerRank Jumping on the Clouds: Revisited

Related Posts

HackerRank Count Scorecards Problem Solution HackerRank Count Scorecards Problem Solution c
HackerRank ByteLandian Tours Problem Solution HackerRank ByteLandian Tours Problem Solution c
HackerRank Similar Strings Problem Solution HackerRank Similar Strings Problem Solution c
HackerRank Drawing Book Problem Solution HackerRank Drawing Book Problem Solution c
HackerRank Counting Road Networks Problem Solution HackerRank Counting Road Networks Solution C++
HackerRank String Reduction Problem Solution HackerRank String Reduction Problem Solution c

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Pick Your Subject
Human Values

Copyright © 2023 TheCScience.

Powered by PressBook Grid Blogs theme