Sequence Equation HackerRank Solution in C, C++, Java, Python

Given a sequence of  integers,  where each element is distinct and satisfies . For each  where , find any integer  such that  and print the value of  on a new line.

For example, assume the sequence . Each value of  between  and , the length of the sequence, is analyzed as follows:

The values for  are .

Function Description

Complete the permutationEquation function in the editor below. It should return an array of integers that represent the values of .

permutationEquation has the following parameter(s):

  • p: an array of integers

Input Format

The first line contains an integer , the number of elements in the sequence.

The second line contains  space-separated integers  where ..

Output Format

For each  from  to , print an integer denoting any valid  satisfying the equation  on a new line.

Sample Input 0

3

2 3 1

 

Sample Output 0

2

3

1

 

 

Sequence Equation HackerRank Solution in C

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>


int main() {
    int i,j,k,n;
    int ar[51];
    scanf("%d",&n);
    for(i=1;i<=n;i++){
        scanf("%d",&ar[i]);
    }
    for(i=1;i<=n;i++){
        for(j=1;j<=n;j++){
            if(ar[j]==i){
                for(k=1;k<=n;k++){
                    if(ar[k]==j)printf("%d\n",k);
                }
            }
        }
    }
    return 0;
}

 

Sequence Equation HackerRank Solution in C++

#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 HackerRank Solution in 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 {
        BufferedReader s1=new BufferedReader(new InputStreamReader(System.in));
        PrintWriter pw=new PrintWriter(System.out);
        int n=Integer.parseInt(s1.readLine().trim());
        String str=s1.readLine().trim();
        String splt[]=str.split(" ");
        HashMap<Integer,Integer> h1=new HashMap<Integer,Integer>();
        for(int i=0;i<n;i++)
        {
            int num=Integer.parseInt(splt[i].trim());
            h1.put(num,i+1);
        }
        for(int i=1;i<=n;i++)
        {
            int get1=h1.get(i);
            int get2=h1.get(get1);
            pw.println(get2);
        }
        pw.flush();
    }
}

 

Sequence Equation HackerRank 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 HackerRank Solution in C#

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

namespace Solution {
class Solution {
    static void Main(string[] args) {
         var n = int.Parse(Console.ReadLine());
            string[] p_temp = Console.ReadLine().Split(' ');
            int[] p = Array.ConvertAll(p_temp, Int32.Parse);

            
            for (int i = 0; i < n; i++)
            {
                var x = i + 1;
                for (int j = 0; j < n; j++)
                {
                    if (x == p[p[j] - 1])
                    {
                        Console.WriteLine(j + 1);
                    }
                }
            }
    }
}
}

 

Attempt Sequence Equation HackerRank Challenge 

Link – https://www.hackerrank.com/challenges/permutation-equation/

Next HackerRank Challenge Solution 

Link – https://exploringbits.com/jumping-on-the-clouds-revisited-hackerrank-solution/

 

Leave a Comment